Golang - Parsing Nested JSON

I use go-worker to handle resque jobs. The job has a payload that has a nested JSON structure as follows:

[{ "key-a":"val-a", "key-b":"val-b", "files":[{ "key-a": [ {"a":"b","c": "d"}, {"e":"f","g": "h"} ], "key-b": [ {"a":"b","c": "d"}, {"e":"f","g": "h"} ] }] }] 

Now go-worker gives me args ...interface{} , which represents the JSON payload, not the actual JSON text. Is there an idiomatic way to convert this ( args ) to the correct types (you can use a different package for this.) Using type statements manually seems a little tedious for such a thing.

+1
source share
1 answer

If this really gives you the actual go objects (that is, the map[string]interface{} bundle rather than the json string itself), then you probably can't do anything but a few types of type statements.

You can reprogram it to json, then parse it again into the correct structures, but this is a bit of a hack (and I have no idea if this will work or not).

+1
source

Source: https://habr.com/ru/post/1216521/


All Articles