-1

golang beginner here.

I want to unmarshall some JSON shown here:

 {
  "intro": {
    "title": "The Little Blue Gopher",
    "story": [
      "Once upon a time, long long ago, there was a little blue gopher. Our little blue friend wanted to go on an adventure, but he wasn't sure where to go. Will you go on an adventure with him?",
      "One of his friends once recommended going to New York to make friends at this mysterious thing called \"GothamGo\". It is supposed to be a big event with free swag and if there is one thing gophers love it is free trinkets. Unfortunately, the gopher once heard a campfire story about some bad fellas named the Sticky Bandits who also live in New York. In the stories these guys would rob toy stores and terrorize young boys, and it sounded pretty scary.",
      "On the other hand, he has always heard great things about Denver. Great ski slopes, a bad hockey team with cheap tickets, and he even heard they have a conference exclusively for gophers like himself. Maybe Denver would be a safer place to visit."
    ],
    "options": [
      {
        "text": "That story about the Sticky Bandits isn't real, it is from Home Alone 2! Let's head to New York.",
        "arc": "new-york"
      },
      {
        "text": "Gee, those bandits sound pretty real to me. Let's play it safe and try our luck in Denver.",
        "arc": "denver"
      }
    ]
  },...}

Into a map[string]Context.

Here are the relevant definitions:

type Context struct {
    title   string
    story   string 
    options *[]Option
}

type Option struct {
    text string
    arc  string
}

The unmarshall runs without error however I get a map[intro] with the Context struct that has everything initialized to nils or empty strings.

What is the correct way of doing this? The documentation and examples are just really hard to parse for specific use cases.

edit: There is another question that is a possible duplicate but this question is a little different as it requires string tags to be introduced in order to work correctly.

Brian Yeh
  • 3,119
  • 3
  • 26
  • 40
  • 1
    Can you provide the structure of the JSON? – MonkeyScript Jan 05 '19 at 04:47
  • 5
    You need to export the fields. – mkopriva Jan 05 '19 at 04:59
  • @Arcteezy the structure of the JSON is shown in the example. It is an Object of objects. The root Object has variable keys. The subObject has "title" "story" and "options" as keys. "title" and "story" have strings as values while "options" contains a variable sized List of Objects with "text" and "arc" as keys. Both "text" and "arc" have strings as values. – Brian Yeh Jan 05 '19 at 05:10
  • 1
    Possible duplicate of [json.Unmarshal returning blank structure](https://stackoverflow.com/questions/28228393/json-unmarshal-returning-blank-structure) – Charlie Tumahai Jan 05 '19 at 06:15
  • 1
    Possible duplicate of [json.Unmarshal returning blank structure](https://stackoverflow.com/questions/28228393/json-unmarshal-returning-blank-structure) – Peter Jan 05 '19 at 08:28
  • Supply a valid json please and example of the expected map. m["intro"]= Context{} ? – ZAky Jan 06 '19 at 07:36

1 Answers1

5

For marshalling and unmarshalling, fields must be exported.

    type Context struct {
        Title   string   `json:"title"`
        Story   string   `json:"story"`
        Options []Option `json:"options"`
    }

    type Option struct {
        Text string `json:"text`
        Arc  string `json:"arc"`
    }
MonkeyScript
  • 4,776
  • 1
  • 11
  • 28
  • 3
    The key here is that he capitalized the struct fields. Fields targetted for marshalling must be exported. – saarrrr Jan 05 '19 at 05:02
  • @saarrrr why does capitalizing the fields change things? What do you mean the fields must be exported? – Brian Yeh Jan 05 '19 at 05:11
  • 3
    @BrianYeh Go uses capitalization to control field/struct export status in a package. Only exported structs and fields are visible to other packages, including the `encoding/json` package. So you must capitalize the field names of the structs to export those fields so the Marshal func can see them. – saarrrr Jan 05 '19 at 05:19