A JSON5 parser that, among other features, supports round-trip preservation of comments
pip install json-fivejson-five
JSON5 for Python
Installation
pip install json-five
This project requires Python 3.8+
Key features
- Supports the JSON5 spec
- Supports similar interfaces to stdlib
jsonmodule - Provides 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)
Usage
NOTE: the import name is json5 which differs from the install name.
For basic loading/dumping, the interface is nearly identical to that of the json module.
import json5
json_text = """{ // This is a JSON5 comment
"foo": "bar", /* this is a JSON5 block
comment that can span lines */
bacon: "eggs" // unquoted Identifiers also work
}
"""
print(json5.loads(json_text))
# {"foo": "bar", "bacon": "eggs"}
with open('myfile.json5') as f:
data = json5.load(f)
For loading JSON5, the same parameters object_hook, object_pairs_hook and parse_* keyword arguments are available
here for load/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 a JsonIdentifier object,
which is a subclass of str (meaning it's compatible anywhere is accepted).
This helps keep keys the same round-trip, rather than converting unquoted identifiers into
strings: