I was writing a web scraper in Nim using the standard libraries. The web page that I was scraping from had some invalid JSON data in it that I needed to use. E.g.:
[{id: 1, name: "alfred"}, {id: 2, name: "blake"}]
Where it should have had the keys encased in string quotes:
[{"id": 1, "name": "alfred"}, {"id": 2, "name": "blake"}]
Some browsers and JSON parsers do accept the first example as valid JSON (even though by the JSON standard's definition it's not). Should this be considered something to add to nim? Or a function to correct invalid JSON (in the json module)?
I think the stdlib Json parser needs a "quirky" flag supporting wild Json. It may even be the default.
Unquoted keys are valid YAML, so you can use NimYAML instead. It has a proc loadToJson that loads input into the stdlib's JSON structure. And as YAML 1.2 is a superset of JSON, it is guaranteed to parse all valid JSON.
YAML also permits other things that are not allowed in JSON but sometimes allowed by implementations, like trailing commas in arrays / objects ([1, 2, ]), so it may be helpful for parsing quirky JSON.
@Araq I was going to propose that. I think strict might be a better name (with the default being false).
@flyx Thanks for the info. I'll use that instead next time.
(I think the default should be a strict parser, with a more tolerant one opt-in)
I don't think this is easy.
Most helpful comment
(I think the default should be a strict parser, with a more tolerant one opt-in)