Are there any Python JSON parsers that will cope with trailing commas?
(I'm consuming the "JSON" from an external source and have no control over it.)
Are there any Python JSON parsers that will cope with trailing commas?
(I'm consuming the "JSON" from an external source and have no control over it.)
Grab PyYAML. JSON is a subset of YAML, so a YAML parser should parse most JSON. YAML's grammar allows trailing commas in sequences.
json-cfg appears to support an extension of JSON that allows it. It also allows comments and unquoted keys.
>>> import jsoncfg
>>> jsoncfg.loads('{"key1": "{my tricky value,}", }')
OrderedDict([('key1', '{my tricky value,}')])
The extra options (comments and unquoted keys) can be disabled with the [JSONParserParams
] class:
jsoncfg.loads('{"key1": "{my tricky value,}" /*nope*/}', jsoncfg.JSONParserParams(allow_comments=False, allow_unquoted_keys=False))
This comes without all the concern about allowing the entire YAML syntax. Furthermore, unlike regex-based preprocessing and overly simple modules such as jsoncomment, it implements a full blown tokenizer and parser (as befits a non-regular language) to avoid nesting problems (like when a comma trails a ]
or }
inside a string).
Whether this library is still maintained or not is an open question. It could definitely use a bit more documentation.