问题
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.)
回答1:
Grab PyYAML. JSON is a subset of YAML, so a YAML parser should parse most JSON. YAML's grammar allows trailing commas in sequences.
回答2:
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.
来源:https://stackoverflow.com/questions/11052952/parsing-json-containing-trailing-commas