Can json.loads ignore trailing commas?

Fast forward to 2021, now we have https://pypi.org/project/json5/

A quote from the link:

A Python implementation of the JSON5 data format.

JSON5 extends the JSON data interchange format to make it slightly
more usable as a configuration language:

  • JavaScript-style comments (both single and multi-line) are legal.
  • Object keys may be unquoted if they are legal ECMAScript identifiers
  • Objects and arrays may end with trailing commas.
  • Strings can be single-quoted, and multi-line string literals are
    allowed.

Usage is consistent with python’s built in json module:

>>> import json5
>>> json5.loads('{"key1": "{my special value,}",}')
{u'key1': u'{my special value,}'}

It does come with a warning:

Known issues

  • Did I mention that it is SLOW?

It is fast enough for loading start up config etc.

Leave a Comment