Unmarshal/Decode dynamic json


So I’m reading about various different ways of dealing with dynamic JSON structures and found a few links that seem to make sense, but I am wondering what possibly the preferred or “best” way is to deal with that kind of JSON ?

Here the links I found which I think are both great, but both use very different approach :
https://www.goinggo.net/2014/01/decode-json-documents-in-go.html (by William Kennedy)
http://eagain.net/articles/go-dynamic-json/ (by Tommi Virtanen)


Both those posts are pretty good.

My process for this is usually:

  1. figure out what I have (i.e., the json input)
  2. figure out what I need (i.e., the go data structures)
  3. figure out how to get what I need from what I have.

Those posts present methods for step 3. @goinggodotnet’s is better for when what you need is scattered about though what you have. Tommi Virtanen’s is better when what you have is mostly what you need (i.e., they have a similar organization). In general there is no best way. The best way always depends on what you have and what you need.

Thanks, that makes sense :slight_smile: ! FWIW, I found a tool that can help turning JSON examples into structs so maybe that’s of interest to some people here … https://github.com/ChimeraCoder/gojson

I used above so I can start playing with https://api.gocd.io/current/#dashboard and start building a custom dashboard … much faster than trying to create all those nested structs myself, although I probably would have use different naming for them but it’s just for getting the data out.

Still finding the overall json->struct process a bit clunky but maybe it’s just a thing to get used to :blush: hehe


1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.