2

I have below JSON

[{
  "id": 8,
  "title": "Indonesia",
  "type": "country",
  "attributes": {
    "regionCode": "ID",
    "information": {
      "title": "Welcome to Indonesia",
      "content": "We only serve selected areas in Indonesia.",
      "image": "indo.png"
    }
  },
  "children": [
    {
      "id": 9,
      "title": "Jakarta",
      "type": "city",
      "attributes": {
        "regionCode": "ID-JKT",
        "information": {
          "title": "Welcome to Capital City of Indonesia",
          "content": "We only serve selected areas in Jabotabek",
          "image": "jakarta.png"
        }
      }       
    },
    {
      "id": 10,
      "title": "Bali",
      "type": "city",
      "attributes": {
        "regionCode": "ID-BAL",
        "information": {
          "title": "Welcome to the beach city Bali",
          "content": "We only serve selected areas in Bali.",
          "image": "bali.png"
        }   
      }       
    }
  ]
}]

Technically this is a nested structure to itself as the children node, and the children might have children as well. But i just couldnt get it right when im using unmarshal to decode this.

The working one is like below i have no problem defining the attribute as a separate struct so assuming i have locationAttribute struct

type locationNode []struct {
    ID         string             `json:"id"`
    Title      string             `json:"title"`
    Type       string             `json:"type"`
    Attributes locationAttribute `json:"attributes"`
    Children   []struct {
        ID         string             `json:"id"`
        Title      string             `json:"title"`
        Type       string             `json:"type"`
        Attributes locationAttribute `json:"attributes"`
        Children   []interface{}      `json:"children"`
    } `json:"children"`
}

However im expecting to be something like below

type locationNode []struct {
    ID         string             `json:"id"`
    Title      string             `json:"title"`
    Type       string             `json:"type"`
    Attributes locationAttribute `json:"attributes"`
    Children   []locationNode `json:"children"`
}

any help will be much appreciated

2
  • What is the question? You can use Children []locationNode and it will work. Commented Jan 6, 2017 at 9:56
  • umm but i got an error {"error":"json: cannot unmarshal object into Go value of type []address.locationNode"} Commented Jan 6, 2017 at 10:08

4 Answers 4

1

Besides the fact that in the JSON text the "id" field is a number and not string, it works for me.

Omitting the "attributes" to make the example shorter:

type locationNode struct {
    ID       int            `json:"id"`
    Title    string         `json:"title"`
    Type     string         `json:"type"`
    Children []locationNode `json:"children"`
}

func main() {
    ln := locationNode{}

    err := json.Unmarshal([]byte(src), &ln)

    fmt.Printf("%+v %v\n", ln, err)
}

const src = `{
  "id": 8,
  "title": "Indonesia",
  "type": "country",
  "attributes": {
    "regionCode": "ID",
    "information": {
      "title": "Welcome to Indonesia",
      "content": "We only serve selected areas in Indonesia.",
      "image": "indo.png"
    }
  },
  "children": [
    {
      "id": 9,
      "title": "Jakarta",
      "type": "city",
      "attributes": {
        "regionCode": "ID-JKT",
        "information": {
          "title": "Welcome to Capital City of Indonesia",
          "content": "We only serve selected areas in Jabotabek",
          "image": "jakarta.png"
        }
      }       
    },
    {
      "id": 10,
      "title": "Bali",
      "type": "city",
      "attributes": {
        "regionCode": "ID-BAL",
        "information": {
          "title": "Welcome to the beach city Bali",
          "content": "We only serve selected areas in Bali.",
          "image": "bali.png"
        }   
      }       
    }
  ]
}`

Output (try it on the Go Playground):

{ID:8 Title:Indonesia Type:country Children:[{ID:9 Title:Jakarta Type:city Children:[]} {ID:10 Title:Bali Type:city Children:[]}]} <nil>
Sign up to request clarification or add additional context in comments.

Comments

1

Using https://mholt.github.io/json-to-go/ your JSON converts to

type AutoGenerated struct {
    ID int `json:"id"`
    Title string `json:"title"`
    Type string `json:"type"`
    Attributes struct {
        RegionCode string `json:"regionCode"`
        Information struct {
            Title string `json:"title"`
            Content string `json:"content"`
            Image string `json:"image"`
        } `json:"information"`
    } `json:"attributes"`
    Children []struct {
        ID int `json:"id"`
        Title string `json:"title"`
        Type string `json:"type"`
        Attributes struct {
            RegionCode string `json:"regionCode"`
            Information struct {
                Title string `json:"title"`
                Content string `json:"content"`
                Image string `json:"image"`
            } `json:"information"`
        } `json:"attributes"`
    } `json:"children"`
}

2 Comments

Thanks for your comment but this is not ideal bcoz technically the children could have children and so on. "children": [ { "id": 9, "title": "Jakarta", "type": "city", "attributes": {}, "children": [{},{},{}] },...
It's important to know that the tool exists plus it should give you a head start in customizing it to your need.
0

It certainly looks like defining Children in the way you expect works fine. The following both marshals and unmarshals fine.

type Bob struct {
    ID         string            `json:"id"`
    Title      string            `json:"title"`
    Type       string            `json:"type"`
    Attributes int               `json:"attributes"`
    Children   []Bob             `json:"children"`
}

Playground link

Comments

0

Okay I managed to resolve this - it's actually a silly careless mistake, since my struct is already an array I should NOT define the children as an array.

So below definition works

type locationNode []struct {
  ID         string             `json:"id"`
  Title      string             `json:"title"`
  Type       string             `json:"type"`
  Attributes locationAttribute  `json:"attributes"`
  Children   locationNode     `json:"children"`
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.