|
Is there a way to easily parse a JSON structure in Go? Let's say I've got:code:
- 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:
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
|
# ¿ Nov 5, 2018 17:43 |
|
|
# ¿ May 12, 2024 06:40 |
|
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 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. Cool! Thank you very much - this makes life much easier. canis minor fucked around with this message at 18:39 on Nov 5, 2018 |
# ¿ Nov 5, 2018 18:31 |
|
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
|
# ¿ Nov 7, 2018 00:31 |
|
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 |
# ¿ Nov 7, 2018 23:54 |
|
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 |
# ¿ Aug 1, 2020 00:56 |