Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
canis minor
May 4, 2011

Is there a way to easily parse a JSON structure in Go? Let's say I've got:

code:
{
    foo: [{
        bar: [{
            qux: ''
        }, {
            quz: ''
        }]
    }, {
        baz: [{
            bax: ''
        }, {
            qur: ''
        }]
    }]
}
Let's assume I want to access foo[0].bar[0].qux - the choices I have is to define:
- a nested struct that will mirror what the json is, so I have to define a custom struct for any json I want to traverse, which I guess becomes tedious quite quickly
- define given object as interface{} and cast it at the point of retrieval - but then traversing json will look like

code:
var jsonResponse interface{}
err = json.Unmarshal(buffer, &jsonResponse)

if err != nil {
	// handle err
}

fmt.Println(jsonResponse.(map[string]interface{})["foo"].([]interface{})[0].(map[string]interface{})["bar"])...
Which is horrendous.

I'm beginning with Go, and I can't find a simple, generic way to achieve what I'm after, with people advocating approach no 1

Adbot
ADBOT LOVES YOU

canis minor
May 4, 2011

bonds0097 posted:

As a strong advocate of schema-first design, I would argue for approach 1. Assuming you use an actual schema like Avro or Protobuf, you can easily leverage code generation to turn all that into Go struct types for you.

I need to mention that I'm digesting an external API, so schema will differ, depending on the parameters. So, no, I don't think so - at least I've not seen any json schema files I could use.

Mao Zedong Thot posted:

Check out https://github.com/mitchellh/mapstructure

You still have to traipse your way through an unknown structure, but mapstructure makes it a bit nicer to do.

Thank you, but I'm confused as I'll still have to define the struct per https://godoc.org/github.com/mitchellh/mapstructure#Decode - I don't think I can do "decode this json to a nested structure that's defined solely within this json".

DARPA posted:

Most of the time I find the best thing to do with Go is just write the code. No clever tricks or shortcuts. Just write out the structs for your JSON and then you'll have first class objects in go to work with.

That said, https://mholt.github.io/json-to-go/ will generate go structs from a json sample.

Cool! Thank you very much - this makes life much easier.

canis minor fucked around with this message at 18:39 on Nov 5, 2018

canis minor
May 4, 2011

ragzilla posted:

With encoding/json you'd handle optional parameters by making them a pointer in the struct and using the 'omitempty' hint so you get a nil for an omitted value.

Yes, I can handle it in different ways - define one big struct to cater for all cases, or define different structs for different scenarios; I just wish I wouldn't need to define the structs at all, but still was able to traverse the given object.

I can understand why it's not possible, but given that I'm a beginner in Go it was worth asking :)

canis minor
May 4, 2011

Yesterday I tried using something that should be simple - which is writing struct into CSV, using https://github.com/gocarina/gocsv library and from the start found 3 errors that prevented me from using it at all, so yay, now I'll be just using standard CSV handling and making that work.

edit: and today I've encountered an issue when double value, retrieved from mongodb using mgo is returned as 0 if I define value as float64, but as correct value when I define it as string.

edit edit: and that's because nonuniform data in mongodb - wheeee

canis minor fucked around with this message at 10:57 on Nov 8, 2018

canis minor
May 4, 2011

When I was learning Go, I've written a utility that would parse the json (what is json but a string in a specific format) into separate files with defined structs. Every object was a file, and then properties converted to estabilished types; if there was a nested object - another file. While I could import those files after the program finished running I couldn't import them dynamically - I didn't simply want to generate JSON structs that would be usable next time the program would run - I wanted them now. Then I started playing with plugin - figuring, hey, let's import those structs as plugin, found out plugins don't allow that and you still end up with interface{} because something something and then got fed up with it all. Was a couple of years ago, so if I'm getting something wrong, I blame it on my broke brain.

I guess, my problem with Go was that even with knowledge of different programming languages, looking at the documentation and after going through tutorials and book about programming in Go I still didn't know what was possible and what wasn't, effectively fighting with the language, because yes, same as you I found it weird that modern language can't import a string in a defined format into a usable struct.

canis minor fucked around with this message at 00:58 on Aug 1, 2020

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply