3 ms·
Is it only me who thinks that parsing JSON is a totally pain in Go ?
by m00dy 11y ago
Is it only me who thinks that parsing JSON is a totally pain in Go ?
- entee 11y agoI haven't used Go, but have spent a lot of time in Scala. Any statically typed language means increased pain with JSON. Static typing really has made me appreciate a well thought out JSON data model where each key in a document can have one and only one type as its value. That said, as someone who spends a lot of time in Python as well, I can't express how much I love the deep autocompletion powers and dumb-mistake suppressing abilities of static typing.
- Zikes 11y agoIt is a bit of a pain, but I've found it to be a worthy trade-off for static typing. Finding documentation on the json struct tags was a lot more difficult than it should've been. This article seems to address a lot of the pain points I originally had: https://eager.io/blog/go-and-json/ https://eager.io/blog/go-and-json/
- grey-area 11y agoYou don't need to parse into a struct, you can also parse it yourself from map[string]interface{} if the JSON is not predictable.
- aikah 11y agoYou can unmarshall a JSON document into a map (map[string]interface{}) or implement some interface and unserialize JSON "by hand" with a method. Go type system is flat so it's often hard to express something without being tempted to "opt-out" from its rigid type system. The question is, is Go type system a good compromise between simplicity and expressiveness ? I guess language design is hard, because the competition (Nim,Rust,...) also have its share of issues.
- bostik 11y agoNot total pain, but certainly loses in convenience to python. Deserializing large and arbitrary JSON inputs is trivial with python's `json.loads()`. Element access rules have no dark corners (as long as you don't have pickled data in there). In contrast, Go and its `json.Unmarshal()` aren't that well suited for parsing arbitrary and/or complex inputs. However, if the expected input is well formed and can be directly mapped to a predefined datastructure, the convenience factor is pretty high. So, from my limited experience, I would say that JSON parsing in golang is more geared towards deserialising well defined transport formats than ingesting arbitrary inputs.
- lowmagnet 11y ago> In contrast, Go and its `json.Unmarshal()` aren't that well suited for parsing arbitrary and/or complex inputs. However, if the expected input is well formed and can be directly mapped to a predefined datastructure, the convenience factor is pretty high. I think go revolves more around well-defined structures, and JSON is anything but. For inter-process pipelines, protobuf (polyglot) and gob (go-only) work better in my experience.
- kaeshiwaza 11y agoIt's different to compare the language and the stdlib.
- girishso 11y agoYes, it's pain! Unless you have the struct ready for unmarshalling json. I use gojson [https://github.com/ChimeraCoder/gojson https://github.com/ChimeraCoder/gojson], it auto generates struct from json with a simple command. curl -s https://api.github.com/repos/chimeracoder/gojson https://api.github.com/repos/chimeracoder/gojson | gojson -name=Repository