json5
A Python implementation of the JSON5 data format.
Description
pyjson5
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.
There are a few other more minor extensions to JSON; see the above page for the full details.
This project implements a reader and writer implementation for Python; where possible, it mirrors the standard Python JSON API package for ease of use.
There is one notable difference from the JSON api: the load() and
loads() methods support optionally checking for (and rejecting) duplicate
object keys; pass allow_duplicate_keys=False to do so (duplicates are
allowed by default).
This is an early release. It has been reasonably well-tested, but it is SLOW. It can be 1000-6000x slower than the C-optimized JSON module, and is 200x slower (or more) than the pure Python JSON module.
Please Note: This library only handles JSON5 documents, it does not allow you to read arbitrary JavaScript. For example, bare integers can be legal object keys in JavaScript, but they aren't in JSON5.
Known issues
-
Did I mention that it is SLOW?
-
The implementation follows Python3's
jsonimplementation where possible. This means that theencodingmethod todump()is ignored, and unicode strings are always returned. -
The
clskeyword argument thatjson.load()/json.loads()accepts to specify a custom subclass ofJSONDecoderis not and will not be supported, because this implementation uses a completely different approach to parsing strings and doesn't have anything like theJSONDecoderclass. -
The
clskeyword argument thatjson.dump()/json.dumps()accepts is also not supported, for consistency withjson5.load(). Thedefaultkeyword is supported, though, and might be able to serve as a workaround.
Contributing
json5 have no runtime dependencies and it is supported on Python version 3.8
or later. However, in order to develop and build the package you need a
bunch of extra tools and the latest versions of those tools may require 3.9
or later. You can install the extra environment on 3.8 (and get older versions
of the tools), but they may not run completely cleanly.
On Mac
The easiest thing to do is to install uv and
use uv and the //run script to develop things. See ./run --help for
the various commands that are supported. glop is the parser generator
tool used to generate a parser from the grammar in json5/json5.g.
$ brew install uv
$ git clone https://github.com/dpranke/pyjson5
$ git clone https://github.com/dpranke/glop
$ cd pyjson5
$ source $(./run devenv) # To activate a venv w/ all the needed dev tools.
On other platforms
Install uv via whatever mechanism is appropriate.
Create the venv
$ ./run devenv
(This calls uv sync --extra dev.)
Running the tests
$ ./run tests
Updating the packages
# Update the version in json5/version.py to $VERSION, which should be of
# the form X.Y.Z where X, Y, and Z are numbers.
$ ./run regen
$ ./run presubmit
$ git commit -a -m "Bump the version to $VERSION"
$ git tag "v$VERSION"
$ ./run build
$ ./run publish --prod
$ git push origin
$ git push --tags origin
(Assuming you have upload privileges to PyPI and the GitHub repo, of course.)
Version History / Release Notes
-
v0.13.0 (2026-01-01)
- No code changes.
- Add Python 3.14 to supported version, project config, dependencies
- Update dependencies to latest stuff < 2025-12-01
- This relaxes the versions specified in pyproject.toml to just use 'newer than' rather than exact matches.
- Sets 'uv.tool.exclude-newer' in pyproject.toml to tell uv not to look at packages published within the past 30 days; this will hopefully help prevent dependencies on compromised projects.
- Switch to using dependency-groups for 'dev' group.
-
v0.12.1 (2025-08-12)
- Fix #94, where objects returned from a custom encoder were not being indented properly.
-
v0.12.0 (2025-04-03)
- Roll back pyproject.toml change for licensing so that we can still build the package on 3.8.
- Upgrade devenv package dependencies to latest versions; they now need Python 3.9 or newer, though json5 itself still supports 3.8.
-
v0.11.0 (2025-04-01)
- Add a couple examples to the documentation and run doctest over them.
- Fix a typing issue in dump and dumps with the
clsargument; turns out mypy was right and I was wrong and I didn't realize it :). - Introduce a new
parsemethod that can be used to iterate through a string, extracting multiple values. - Add a new
consume_trailingparameter toload/loads/parsethat specifies whether to keep parsing after a valid object is reached. By default, this is True and the string must only contain trailing whitespace. If set to False, parsing will stop when a valid object is reached. - Add a new
startparameter toload/loads/parseto specify the zero-based offset to start parsing the string or file from. - GitHub issue #60. Fix a bug where we were attempting to allow '--4' as a valid number.
-
v0.10.0 (2024-11-25)
- GitHub issue #57.
Added a
JSON5Encoderclass that can be overridden to do custom encoding of values. This class is vaguely similar to theJSONEncoderclass in the standardjsonlibrary, except that it has anencode()method that can be overridden to customize any value, not just ones the standard encoder doesn't know how to handle. It does also support adefault()method that can be used to encode things not normally encodable, like the JSONEncoder class. It does not support aniterencodemethod. One could probably be added in the future, although exactly how that would work and interact withencodeis a little unclear. - Restructured the code to use the new encoder class; doing so actually allowed me to delete a bunch of tediously duplicative code.
- Added a new
quote_styleargument todump()/dumps()to control how strings are encoded by default. For compatibility with older versions of the json5 library and the standard json library, it usesQuoteStyle.ALWAYS_DOUBLEwhich encodes all strings with double quotes all the time. You can also configure it to use single quotes all the time (ALWAYS_SINGLE), and to switch between single and double when doing so eliminates a need to escape quotes (PREFER_SINGLEandPREFER_DOUBLE). This also adds a--quote-styleargument topython -m json5. - This release has a fair number of changes, but is intended to be completely backwards-compatible. Code without changes should run exactly as it did before.
- GitHub issue #57.
Added a
-
v0.9.28 (2024-11-11)
- Fix GitHub CI to install
uvso./run testsworks properly. - Mark Python3.13 as supported in package metadata.
- Update dev package dependencies (note that the latest versions of coverage and pylint no longer work w/ Python3.8)
- Fix GitHub CI to install
-
v0.9.27 (2024-11-10)
- Fix typo in //README.md
-
v0.9.26 (2024-11-10)
- GitHub issue #82
Add support for the
strictparameter toload()/loads(). - Significantly rework the infra and the
runscript to be contemporary.
- GitHub issue #82
Add support for the
-
v0.9.25 (2024-04-12)
- GitHub issue #81 Explicitly specify the directory to use for the package in pyproject.toml.
-
v0.9.24 (2024-03-16)
- Update GitHub workflow config to remove unnecessary steps and run on pull requests as well as commits.
- Added note about removing
hypothesizein v0.9.23. - No code changes.
-
v0.9.23 (2024-03-16)
- Lots of cleanup:
- Removed old code needed for Python2 compatibility.
- Removed tests using
hypothesize. This ran model-based checks and didn't really add anything useful in terms of coverage to the test suite, and it introduced dependencies and slowed down the tests significantly. It was a good experiment but I think we're better off without it. - Got everything linting cleanly with pylint 3.1 and
ruff checkusing ruff 0.3.3 (Note that commit message in 00d73a3 says pylint 3.11, which is a typo). - Code reformatted with
ruff format - Added missing tests to bring coverage up to 100%.
- Lots of minor code changes as the result of linting and coverage testing, but no intentional functional differences.
- Lots of cleanup:
-
v0.9.22 (2024-03-06)
- Attempt to fix the GitHub CI configuration now that setup.py is gone. Also, test on 3.12 instead of 3.11.
- No code changes.
-
v0.9.21 (2024-03-06)
- Moved the benchmarks/*.json data files' license information to //LICENSE to (hopefully) make the Google linter happy.
-
v0.9.20 (2024-03-03)
- Added
json5.__version__in addition tojson5.VERSION. - More packaging modernization (no more setup.{cfg,py} files).
- Mark Python3.12 as supported in project.classifiers.
- Updated the
//runscript to use python3.
- Added
-
v0.9.19 (2024-03-03)
- Replaced the benchmarking data files that came from chromium.org with three files obtained from other datasets on GitHub. Since this re