Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing "JSON" containing trailing commas

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

like image 601
Acorn Avatar asked Jan 16 '23 09:01

Acorn


2 Answers

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

like image 99
kwatford Avatar answered Jan 18 '23 23:01

kwatford


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.

like image 41
jpmc26 Avatar answered Jan 18 '23 22:01

jpmc26