3 ms·
Your example is more apt than intended: That's not valid json, which only allows string keys. If you use a library it'll either barf now or later when they fi
by Izkata 6y ago
Your example is more apt than intended: That's not valid json, which only allows string keys. If you use a library it'll either barf now or later when they fix it, so if you're forced to work with an API like that and can't change it, a custom parser is really the only way to go.
- JackFr 6y agoWell the good libraries have option flags that will allow you to handle JSON as found in the wild.
- majewsky 6y agoThat's not JSON, though. It's absolutely something else. Maybe a JS snippet. Maybe YAML. Definitely not JSON, though. (Some JSON libraries do have option flags, but usually it's about whether, during deserializing into a known type, unknown fields are an error or silently ignored. Or whether C-style comments are an error or considered as whitespace.)
- JackFr 6y agoIt may not be JSON but it’s out there in the wild. A lot. Let’s say I’m scraping a website. I can: 1) complain to the owner that “it’s not JSON”. 2) write a parser for a syntax that has no spec, (it’s not JSON, but it sure looks an awful lot like JSON with unquoted keys.) or 3) Set ALLOW_UNQUOTED_FIELD_NAMES to true in the Jackson library.
- Izkata 6y ago> Maybe a JS snippet. While acceptable, also misleading: JS only does string keys, but unlike JSON it'll convert whatever it's given into strings. Not a problem most of the time, since it'll do the same conversion for both accessing and setting, but good to be aware of if you're doing something like iterating Object.keys()
- jimmynest 6y agoI use https://jsonformatter.org/json-parser https://jsonformatter.org/json-parser sometime to parse json and to validate.