0

I'm trying to decode a json response with a struct type. An instance of the object i'm trying to decode looks as follows:

{ "title": "Some Title", "views": 344, "profiles": { "customField": "somevalue", "customField2:" :somevalue" } } 

The golang struct is the following:

type Topic struct { Title string `json:"title"` Views string `json:"views"` Profiles string `json:"profiles"` } 

As you can see, the "Profiles" attribute is a string, since the profiles object is unknown, as the fields inside it can be dinamically defined.

I'm trying to decode this with:

json.NewDecoder(response.Body).Decode(result) 

Where result is of type Topic, but isn't working. What type should the "Profiles" attribute be in order to correctly decoding the answer?

Thanks!

3
  • Are the "profiles" values always strings? Commented Feb 24, 2017 at 15:20
  • Do you have a list of fields in profile that you are interested in. Or you have to read all the fields and operate on them no matter what they are? Commented Feb 24, 2017 at 15:21
  • Profiles values could be anything. The only thing I need is to be able to send that response to a web client, without modifying or reading anything (i'm retrieving that value from a third party api and then returning to the client) Commented Feb 24, 2017 at 15:24

4 Answers 4

3

Reading the comment it's clear that profiles value could be of any type, for this reason I suggest you to declare the Profiles type as a map[string]interface{}.
Topic becomes:

type Topic struct { Title string `json:"title"` Views int32 `json:"views"` Profiles map[string]interface{} `json:"profiles"` } 
Sign up to request clarification or add additional context in comments.

1 Comment

You have a typo in the struct. Should be "string". I can't propose an edit with less than 10 changed characters though.
0

Check out https://github.com/mitchellh/mapstructure The readme has an answer you probably look for.

problem is if you have configuration or an encoding that changes slightly depending on specific fields.

Perhaps we can't populate a specific structure without first reading the "type" field from the JSON. We could always do two passes over the decoding of the JSON (reading the "type" first, and the rest later). However, it is much simpler to just decode this into a map[string]interface{} structure, read the "type" key, then use something like this library to decode it into the proper structure.

Comments

0

If profiles can vary you should take json.RawMessage here. Internally it is a []byte which late can be unmarshalled into other types, e.g. depending on values of the outer document.

See https://golang.org/pkg/encoding/json/#RawMessage and the examples.

Comments

0

The Profiles should be a struct and initiated along with the container, in this case I assume it's a Status, such as FB status or Tweet, I've made an example here https://play.golang.org/p/tG90idakLP

Remember to instantiate the new profiles inside a newly created status, before you start unmarshalling.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.