0

I have a RESTful service that returns response similar to show below:

"Basket" : {
  "Count": 1,
  "Fruits": {[
    {
      "Name":"Mango", 
      "Season":"Summer"
    },
    {
      "Name":"Fig", 
      "Season":"Winter"}
  ]}
}

I am trying to create Go lang model to unmarshal the contents. Following is the code I have tried:

type Response struct {
    Count   int
    Fruits []Fruit
}

type Fruit struct {
    Name string
    Season string
}

But when I marshal the Response object in my test code I don't see similar json. (https://play.golang.org/p/EGKqfbwFvW) Marshalled data always appears as :

{
  "Count":100,
  "Fruits":[
    {"Name":"Mango","Season":"Summer"},
    {"Name":"Fig","Season":"Winter"}
  ]
}

Notice the Fruits appearing as array [] and not {[]} in original json. How can I model structs in golang for this response?

1
  • 5
    That isn't valid JSON. Fruits needs to be either an array or a name: value pair. Commented Feb 2, 2017 at 0:02

2 Answers 2

2

Your model is totally correct and valid, but the JSON object is not. "Fruits" doesn't have name if it should be key value pair or it should be wrapped in [] not {}.

JSON obj should be formatted like this:

{
  "Basket" : {
    "Count": 1,
    "Fruits": [
      {
        "Name":"Mango", 
        "Season":"Summer"
      },
      {
        "Name":"Fig", 
        "Season":"Winter"
      }
    ]
  }
}

And actually invalid json shouldn't work https://play.golang.org/p/yoW7t4NfI7

1
  • You are spot on, the JSON I am consuming comes from a 3rd party and is invalid. I switch to XML and my structure works perfectly (This is why I like Go, transition is just 2 line change). I also validated with json.RawMessage. Json is not supposed to have "[" after "{".
    – sukkad
    Commented Feb 2, 2017 at 18:54
1

I would make 'Baskets' a struct within 'Response', create a 'BasketsData' struct, and give it all some labels.

type Fruit struct {
    Name   string `json:"Name"`
    Season string `json:"Season"`
}

type BasketData struct {
    Count  int     `json:"Count"`
    Fruits []Fruit `json:"Fruits"`
}

type Response struct {
    Basket BasketData `json:"Basket"`
}

This way you will get a top level JSON response when you marshall it.

fruitmania := []Fruit{{Name: "Mango", Season: "Summer"},
                       {Name: "Fig", Season: "Winter"}}
basket := Response{BasketData{Count: 100, Fruits: fruitmania}}

b, _ := json.Marshal(basket)
fmt.Println(string(b))

checkit-checkit out: https://play.golang.org/p/TuUwBLs_Ql

2
  • This does not solve the issue of Fruits appearing as array [] and not {[]}. Output should have "Fruits":{[{"Name":"Mango","Season":"Summer"},{"Name":"Fig","Season":"Winter"}]}
    – sukkad
    Commented Feb 2, 2017 at 2:35
  • @sukkad that's not valid JSON Commented Feb 2, 2017 at 4:45

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.