Parsing “JSON” containing trailing commas

♀尐吖头ヾ 提交于 2019-12-10 16:07:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!