Parsing “JSON” containing trailing commas

2019-06-24 04:22发布

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.)

2条回答
聊天终结者
2楼-- · 2019-06-24 05:11

Grab PyYAML. JSON is a subset of YAML, so a YAML parser should parse most JSON. YAML's grammar allows trailing commas in sequences.

查看更多
太酷不给撩
3楼-- · 2019-06-24 05:18

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.

查看更多
登录 后发表回答