3
{
    "meta": {
       "type": "RESPONSE",
       "application": "",
       "data0": {
         some data
       },
      "lv1": [
      {
        "status": "SUCCESS",
        "response-type": "JSON",
        "message": {},
        "response": {
        "more_data": "TRUE",
        "no_result": "5",
        "current_page": "1",
        "data": [[
         "1",
         "2", 
         "3"]]
        }
      }
     ]
   }
}

type response struct {
    META struct {
        LV []struct {
            RESPONSE struct {
                Data []struct {
                    array []struct {
                        val []string
                    }
                } `json:"data"`
            } `json:"response"`
        } `json:"lv1"`
    } `json:"meta"`

}

How can I get the values in the following?

"data": [[
         "1",
         "2", 
         "3"]]

I've tried both interface and struct. Using interface results in [1 2 3] of interface type and I'm not sure how I can get the values. When using struct, I ran into problem when trying to map an array of array with error message:

"cannot unmarshal array into Go struct field .data of type struct { vals []string }"

1
  • 1
    The JSON you posted is invalid somewhere... Commented Sep 8, 2017 at 20:44

1 Answer 1

15

It's an array of arrays of strings, not an array of structs containing arrays of structs containing strings, so you want something more like:

type response struct {
    Meta struct {
        Lv []struct {
            Response struct {
                Data [][]string `json:"data"`
            } `json:"response"`
        } `json:"lv1"`
    } `json:"meta"`
}

(I also changed the all-caps field names to match expected Go code style.)

For what it's worth, there is a handy JSON-to-Go tool here which gave me this for your input, after deleting the some data bit (which made the JSON invalid):

type AutoGenerated struct {
    Meta struct {
        Type        string `json:"type"`
        Application string `json:"application"`
        Data0       struct {
        } `json:"data0"`
        Lv1 []struct {
            Status       string `json:"status"`
            ResponseType string `json:"response-type"`
            Message      struct {
            } `json:"message"`
            Response struct {
                MoreData    string     `json:"more_data"`
                NoResult    string     `json:"no_result"`
                CurrentPage string     `json:"current_page"`
                Data        [][]string `json:"data"`
            } `json:"response"`
        } `json:"lv1"`
    } `json:"meta"`
}
Sign up to request clarification or add additional context in comments.

2 Comments

The tool you linked to is incredible, thanks for sharing. I just had to delete some duplicate definitions but otherwise it generated a perfect struct. Thanks again!
Agreed! This is actually an awesome tool - saved me sooo many hours

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.