FAQ
Hello,

I've been trying to get a simple JSON to "unmarshal" for several hours
without much success.
Can someone give me some pointers as to what I'm doing wrong?
The output prints out an empty STRUCT array when I expect values from the
JSON to be in the there (Struct array).

Appreciate any help.

http://play.golang.org/p/jox0NLYtP7
------------------------------------------------------------

package main

import (
"encoding/json"
"fmt"
)

var jsonStr = []byte(`
{
   "d": {
     "results": [
       {
         "ID": "id1",
         "Title": "title1",
         "Url": "url1",
         "Source": "sourc1",
         "Description": "desc1",
         "Date": "2013-11-29T15:16:38Z"
       },
       {
         "ID": "id2",
         "Title": "title2",
         "Url": "url2",
         "Source": "sourc2",
         "Description": "desc2",
         "Date": "2013-11-29T15:16:38Z"
       }
     ]
   }
}
`)

type Result []struct {
ID string `json:"ID"`
Title string `json:"Title"`
Url string `json:"URL"`
Source string `json:"Source"`
Description string `json:"Description"`
}
type D struct {
ResultSet Result
}

func main() {
var parsedMap D
err := json.Unmarshal(jsonStr, &parsedMap)
if err != nil {
fmt.Println("ERROR:", err)
}
fmt.Println("SUCCESS:", parsedMap.ResultSet)
}

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Dmitry Vyukov at Dec 1, 2013 at 7:16 am
    your json contains filed 'd' on top level, but the struct you pass to
    unmarshal does not contain field 'd'.
    this works:
    http://play.golang.org/p/LaNd-3h7RN

    On Sun, Dec 1, 2013 at 10:59 AM, Daniel Combs wrote:
    Hello,

    I've been trying to get a simple JSON to "unmarshal" for several hours
    without much success.
    Can someone give me some pointers as to what I'm doing wrong?
    The output prints out an empty STRUCT array when I expect values from the
    JSON to be in the there (Struct array).

    Appreciate any help.

    http://play.golang.org/p/jox0NLYtP7
    ------------------------------------------------------------

    package main

    import (
    "encoding/json"
    "fmt"
    )

    var jsonStr = []byte(`
    {
    "d": {
    "results": [
    {
    "ID": "id1",
    "Title": "title1",
    "Url": "url1",
    "Source": "sourc1",
    "Description": "desc1",
    "Date": "2013-11-29T15:16:38Z"
    },
    {
    "ID": "id2",
    "Title": "title2",
    "Url": "url2",
    "Source": "sourc2",
    "Description": "desc2",
    "Date": "2013-11-29T15:16:38Z"
    }
    ]
    }
    }
    `)

    type Result []struct {
    ID string `json:"ID"`
    Title string `json:"Title"`
    Url string `json:"URL"`
    Source string `json:"Source"`
    Description string `json:"Description"`
    }
    type D struct {
    ResultSet Result
    }

    func main() {
    var parsedMap D
    err := json.Unmarshal(jsonStr, &parsedMap)
    if err != nil {
    fmt.Println("ERROR:", err)
    }
    fmt.Println("SUCCESS:", parsedMap.ResultSet)
    }

    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Alex Zorin at Dec 1, 2013 at 7:55 am
    I wonder (for my personal benefit), what is the best way to go when you
    don't control the structure of the JSON (such as when consuming an external
    API).

    It's possible to unmarshal using a map part of the way (such as in
    http://play.golang.org/p/zgE4jwDBbv), but that's fairly nasty. Any
    improvements?

    Alex
    On Sunday, December 1, 2013 6:16:36 PM UTC+11, Dmitry Vyukov wrote:

    your json contains filed 'd' on top level, but the struct you pass to
    unmarshal does not contain field 'd'.
    this works:
    http://play.golang.org/p/LaNd-3h7RN

    On Sun, Dec 1, 2013 at 10:59 AM, Daniel Combs wrote:
    Hello,

    I've been trying to get a simple JSON to "unmarshal" for several hours
    without much success.
    Can someone give me some pointers as to what I'm doing wrong?
    The output prints out an empty STRUCT array when I expect values from the
    JSON to be in the there (Struct array).

    Appreciate any help.

    http://play.golang.org/p/jox0NLYtP7
    ------------------------------------------------------------

    package main

    import (
    "encoding/json"
    "fmt"
    )

    var jsonStr = []byte(`
    {
    "d": {
    "results": [
    {
    "ID": "id1",
    "Title": "title1",
    "Url": "url1",
    "Source": "sourc1",
    "Description": "desc1",
    "Date": "2013-11-29T15:16:38Z"
    },
    {
    "ID": "id2",
    "Title": "title2",
    "Url": "url2",
    "Source": "sourc2",
    "Description": "desc2",
    "Date": "2013-11-29T15:16:38Z"
    }
    ]
    }
    }
    `)

    type Result []struct {
    ID string `json:"ID"`
    Title string `json:"Title"`
    Url string `json:"URL"`
    Source string `json:"Source"`
    Description string `json:"Description"`
    }
    type D struct {
    ResultSet Result
    }

    func main() {
    var parsedMap D
    err := json.Unmarshal(jsonStr, &parsedMap)
    if err != nil {
    fmt.Println("ERROR:", err)
    }
    fmt.Println("SUCCESS:", parsedMap.ResultSet)
    }

    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts...@googlegroups.com <javascript:>.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Peter Bourgon at Dec 1, 2013 at 10:43 am

    On Sun, Dec 1, 2013 at 8:54 AM, Alex Zorin wrote:
    I wonder (for my personal benefit), what is the best way to go when you
    don't control the structure of the JSON (such as when consuming an external
    API).
    An external API will still return JSON with a defined structure. Your
    consuming code should define a struct that mirrors that structure to
    the degree you care about, which you can Unmarshal into.
    It's possible to unmarshal using a map part of the way (such as in
    http://play.golang.org/p/zgE4jwDBbv), but that's fairly nasty. Any
    improvements?
    http://play.golang.org/p/5xgv_mFHLz

    On Sunday, December 1, 2013 6:16:36 PM UTC+11, Dmitry Vyukov wrote:

    your json contains filed 'd' on top level, but the struct you pass to
    unmarshal does not contain field 'd'.
    this works:
    http://play.golang.org/p/LaNd-3h7RN

    On Sun, Dec 1, 2013 at 10:59 AM, Daniel Combs wrote:
    Hello,

    I've been trying to get a simple JSON to "unmarshal" for several hours
    without much success.
    Can someone give me some pointers as to what I'm doing wrong?
    The output prints out an empty STRUCT array when I expect values from
    the
    JSON to be in the there (Struct array).

    Appreciate any help.

    http://play.golang.org/p/jox0NLYtP7
    ------------------------------------------------------------

    package main

    import (
    "encoding/json"
    "fmt"
    )

    var jsonStr = []byte(`
    {
    "d": {
    "results": [
    {
    "ID": "id1",
    "Title": "title1",
    "Url": "url1",
    "Source": "sourc1",
    "Description": "desc1",
    "Date": "2013-11-29T15:16:38Z"
    },
    {
    "ID": "id2",
    "Title": "title2",
    "Url": "url2",
    "Source": "sourc2",
    "Description": "desc2",
    "Date": "2013-11-29T15:16:38Z"
    }
    ]
    }
    }
    `)

    type Result []struct {
    ID string `json:"ID"`
    Title string `json:"Title"`
    Url string `json:"URL"`
    Source string `json:"Source"`
    Description string `json:"Description"`
    }
    type D struct {
    ResultSet Result
    }

    func main() {
    var parsedMap D
    err := json.Unmarshal(jsonStr, &parsedMap)
    if err != nil {
    fmt.Println("ERROR:", err)
    }
    fmt.Println("SUCCESS:", parsedMap.ResultSet)
    }

    --
    You received this message because you are subscribed to the Google
    Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send
    an
    email to golang-nuts...@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • TR NS at Dec 1, 2013 at 12:34 pm

    On Sunday, December 1, 2013 5:42:59 AM UTC-5, Peter Bourgon wrote:
    On Sun, Dec 1, 2013 at 8:54 AM, Alex Zorin <al...@zor.io <javascript:>>
    wrote:
    I wonder (for my personal benefit), what is the best way to go when you
    don't control the structure of the JSON (such as when consuming an external
    API).
    An external API will still return JSON with a defined structure. Your
    consuming code should define a struct that mirrors that structure to
    the degree you care about, which you can Unmarshal into.
    What do you do for a map with random keys?:

         { "s5fsdf": "foo",
           "ap34m": "bar" }

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Peter Bourgon at Dec 1, 2013 at 12:46 pm
    An API that produces random JSON keys is probably broken and should be fixed.

    On the tiny chance that those semantics are required, you can use a
    map[string]ValueType.

       {"r": {"s5fsdf": "foo", "ap34m": "bar"}}

       type Response struct {
           R map[string]string `json:"r"`
       }

    If both key names and value types are arbitrary, you have no choice
    but to use map[string]interface{} and do run-time reflection on the
    values. But I'd love to see an example of an API that has those
    semantics by necessity, because I haven't (yet).

    On Sun, Dec 1, 2013 at 1:34 PM, TR NS wrote:

    On Sunday, December 1, 2013 5:42:59 AM UTC-5, Peter Bourgon wrote:
    On Sun, Dec 1, 2013 at 8:54 AM, Alex Zorin wrote:
    I wonder (for my personal benefit), what is the best way to go when you
    don't control the structure of the JSON (such as when consuming an
    external
    API).
    An external API will still return JSON with a defined structure. Your
    consuming code should define a struct that mirrors that structure to
    the degree you care about, which you can Unmarshal into.
    What do you do for a map with random keys?:

    { "s5fsdf": "foo",
    "ap34m": "bar" }
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Felix Zilla at Dec 2, 2013 at 12:30 am
    This will work
    or as anonymous struct
    http://play.golang.org/p/Zr2CFn99hD

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedDec 1, '13 at 7:00a
activeDec 2, '13 at 12:30a
posts7
users6
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase