Package: json-five
json-fiveJSON5 for PythonInstallationpip install json-fiveThis project requires Python 3.8+Key featuresSupports the JSON5 specSupports similar interfaces to stdlibjsonmoduleProvides an API for working with abstract model representations of JSON5 documents.Supports round-trip loading, editing, and dumping, preserving non-data elements such as comments (in model-based load/dump)UsageNOTE:the import name isjson5which differs from the install name.For basic loading/dumping, the interface is nearly identical to that of thejsonmodule.importjson5json_text="""{ // This is a JSON5 comment"foo": "bar", /* this is a JSON5 blockcomment that can span lines */bacon: "eggs"  // unquoted Identifiers also work}"""print(json5.loads(json_text))# {"foo": "bar", "bacon": "eggs"}withopen('myfile.json5')asf:data=json5.load(f)For loading JSON5, the same parametersobject_hook,object_pairs_hookandparse_*keyword arguments are available
here forload/loads.Additionally, a new hook,parse_json5_identifiers, is available to help users control the
output of parsing identifiers. By default, JSON5 Identifiers in object keys are returned as aJsonIdentifierobject,
which is a subclass ofstr(meaning it's compatible anywherestris accepted).
This helps keep keys the same round-trip, rather than converting unquoted identifiers into
strings:>>>text='{bacon: "eggs"}'>>>json5.dumps(json5.loads(text))==textTrueYou can change this behavior with theparse_json5_identifiersargument with a callable that receives theJsonIdentifierobject
and its return value is used instead. For example, you can specifyparse_json5_identifiers=strto convert identifiers
to strings.>>>json5.dumps(json5.loads(text,parse_json5_identifiers=str))'{"bacon": "eggs"}'Custom loaders; Abstract JSON5 ModelsNote:the underlying model API and tokens are not stable and are subject to breaking changes, even in minor releases.json-five also features an API for representing JSON5 as an abstract model. This enables a wide degree of capabilities for
various use-cases, such as linters, formatters, custom serialization/deserialization, and more.Example: a simple modelfromjson5.loaderimportloads,ModelLoaderjson_string="""{foo: "bar"}"""model=loads(json_string,loader=ModelLoader())The resulting model object looks something like this:JSONText(value=JSONObject(keys=[Identifier(name="foo",raw_value="foo")],values=[DoubleQuotedString(characters="bar",raw_value='"bar"')],trailing_comma=None,))It is possible to make edits to the model, which will affect the output when dumped using the model dumper. However,
there is (currently) no validation to ensure your model edits won't result in invalid JSON5 when dumped.You may also implement custom loaders and dumpers to control serialization and deserialization. See thefull documentationfor more information.TokenizationYou can also leverage tokenization of JSON5:fromjson5.tokenizerimporttokenizejson_string="""{foo: "bar"}"""fortokintokenize(json_string):print(tok.type)Output would be:LBRACE
NAME
COLON
WHITESPACE
DOUBLE_QUOTE_STRING
RBRACEStatusThis project currently fully supports the JSON5 spec and its interfaces for loading and dumping JSON5 is stable as of v1.0.0.
There is still active development underway, particularly for the underlying abstract JSON5 model representations and
ability to perform edits using the abstract model.