3 ms·
> Python doesn't do typing well Golang does typing, but JSONs are PITA to handle. Try parsing something like `[{"a': 1, "b": "c", "d": [], "e": {}}, null, 1,
by est 2y ago
> Python doesn't do typing well
Golang does typing, but JSONs are PITA to handle.
Try parsing something like `[{"a': 1, "b": "c", "d": [], "e": {}}, null, 1, "2"]` in go.
Types are a bless as well as a curse.
- Aditya_Garg 2y agoThats only because your list has different types. Its a badly formed API and if you really need to support that use case then you can use maps and reflection to handle it.
- est 2y agoThe problem is, programmers can't dictate what JSON should look like in the wild. We used to have strict typed XML. Nobody even bothered.
- a57721 2y ago> The problem is, programmers can't dictate what JSON should look like in the wild. Not JSONs in general, but a sane API would never return something like that. > We used to have strict typed XML. Nobody even bothered. Nowadays there is OpenAPI, GraphQL, protobuf, etc. and people do bother about such things.
- mook 2y agoUnfortunately, a lot of the time you need to deal with other people's APIs.
- shiroiushi 2y ago>We used to have strict typed XML. Nobody even bothered. Yeah, because it was ugly as hell and not human-readable.
- Turskarama 2y agoAnd if you got that JSON back in Python, how would you do anything with it? This API is essentially useless. You can deserisalise it, sure, but then what?
- est 2y agoI can get parsing job easily done without mental gymnastics.
- Turskarama 2y agoRight but what do you do with the parsed object? An array of random objects is used for what, exactly?
- jpc0 2y ago[]inferface But the same issue exists as other dynamic languages, how do you know what the type is of the item you are accessing? If you know the array will be laid out exactly like that before you make the request you can always create a custom parser to return a struct with those fields name what they actually are instead of arbitrary data. The only valid way to parse that dynamically is to try and fail in a loop which is inefficient enough that you should stop using whatever API returns that monstrosity.
- emmanueloga_ 2y agogjson [1] and a few other go packages offer a way to parse arbitrary JSON without requiring structs to hold them. re: Python. I like PyRight/PyLance for Python typing, it seems to "just work" afaict. I also like msgspec for dataclass like behavior [2]. --- 1: https://github.com/tidwall/gjson https://github.com/tidwall/gjson 2: https://jcristharif.com/msgspec/ https://jcristharif.com/msgspec/