A fast JSON5 encoder/decoder for Python
Project description
ujson5
| CI/CD | |
| Coverage / Codspeed | |
| Package | |
| Meta |
ujson5 is a Python that encodes and decodes JSON5, a superset of JSON that supports many human-friendly features such as comments, trailing commas, and more!
Why use JSON5?
Direct quote from the JSON5 website:
JSON5 was started in 2012, and as of 2022, now gets >65M downloads/week, ranks in the top 0.1% of the most depended-upon packages on npm, and has been adopted by major projects like Chromium, Next.js, Babel, Retool, WebStorm, and more. It's also natively supported on Apple platforms like macOS and iOS.
Why use ujson5?
- Gentle learning curve - If you know how to use the
jsonmodule in Python, you already know how to useujson5.ujson5API is almost identical to thejsonmodule with some additional features. - Robust test suite -
ujson5is tested against the most famous JSON5 test suite to ensure compatibility. See the testing section for more information. - Speed -
ujson5tokenizer and parser implement DFA-based algorithms for fast parsing, which is only slightly slower than the built-injsonmodule. - Pythonic - Comments in python are directly encoded into JSON5 comments. Magic!
- Quality code base -
ujson5is linted withflake8, formatted withblack, and type-checked withmypy. What's more? 100% test coverage withpytestandcodecov! - Friendly Error Messages -
ujson5provides detailed error messages to help you debug your JSON5 files, including the exact location of the error. - Type hints -
ujson5provides type hints for all public functions and classes.
Installation
pip install ujson5
Quick Start
You can use ujson5 just like the built-in json module. Here is a quick example:
from typing import TypedDict
import ujson5
# Decode JSON5
json5_str = """
{
// comments
key: 'value', // trailing comma
}
"""
data = ujson5.loads(json5_str)
print(data) # {'key': 'value'}
# Encode JSON5
class Data(TypedDict):
# multiple long comments
# are supported
key: str # inline comment
data = {"key": "value"}
json5_str = ujson5.dumps(data, Data, indent=2)
print(json5_str)
# {
# // multiple long comments
# // are supported
# "key": "value", // inline comment
# }
CLI Usage
After installing ujson5, you can use the ujson5 command-line interface to convert JSON5 files to JSON files or simply validate JSON5 files. The CLI interface is the same as the official JSON5 CLI.
Installation
Make sure you have installed the package in these three ways to use the CLI:
- Install using pipx (recommended):
pipx install ujson5 - Install to the global interpreter:
pip install ujson5 - Install to a virtual env and activate it.
Usage
ujson5 module can be used as a console script. The basic usage is:
ujson5 <infile> <outfile> [options]
If the optional infile and outfile arguments are not specified, sys.stdin and sys.stdout will be used respectively:
echo '{"json": "obj"}' | ujson5
{
"json": "obj"
}
Options:
-
infile: The JSON5 file to be validated or converted to JSON. If not specified, read fromsys.stdin. -
outfile: The JSON file to output the converted JSON. If not specified, output tosys.stdout. -
-v,--version: Print the version number. -
-i,--info: Print the version number and system information. -
--sort-keys: Sort the output of dictionaries alphabetically by key. -
--no-ensure-ascii: Disable escaping of non-ascii characters, see [ujson5.dumps()][ujson5.dumps] for more information. -
--indent,--no-indent,--compact: Mutually exclusive options for whitespace control. -
-h,--help: Output usage information
Testing
ujson5 is tested against fixtures in the JSONTestSuite, nativejson-benchmark, and json5-tests repositories. It is tested to not crash against the Big List of Naughty Strings.
Changelog
v1.0.1 (2025-03-19)
What's Changed
- Makefile automation by @austinyu in #13
- Fixed incorrect comments extraction by @austinyu in #18
- Trunk quality compliance by @austinyu in #19
- Release pipeline done by @austinyu in #21
v1.0.0a (2025-03-07)
What's Changed
- Fixed wrong wheel name generated by shallow clone repo in CI by @austinyu in https://github.com/austinyu/ujson5/pull/7
- Migrating to uv and hatch as project management tool and build tool. by @austinyu in https://github.com/austinyu/ujson5/pull/8
- Improve on package metadata by @austinyu in https://github.com/austinyu/ujson5/pull/10
New Contributors
- @austinyu made their first contribution in https://github.com/austinyu/ujson5/pull/7
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ujson5-1.0.1.tar.gz.
File metadata
- Download URL: ujson5-1.0.1.tar.gz
- Upload date:
- Size: 752.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
df8ba1b1052bc7dd0504c5db7a016c527aff9ea53618525069d1f901322fd15d
|
|
| MD5 |
2000b5bc8d747ffb14a194119a7950b9
|
|
| BLAKE2b-256 |
9bedd3761b2b6d75399d3fa0528e72e88319eff63873d4c99fdc70afaabed521
|
Provenance
The following attestation bundles were made for ujson5-1.0.1.tar.gz:
Publisher:
release.yml on austinyu/ujson5
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ujson5-1.0.1.tar.gz -
Subject digest:
df8ba1b1052bc7dd0504c5db7a016c527aff9ea53618525069d1f901322fd15d - Sigstore transparency entry: 184709447
- Sigstore integration time:
-
Permalink:
austinyu/ujson5@184276c81a2874427268f4fe0f3e2793d873c022 -
Branch / Tag:
refs/tags/v1.0.1 - Owner: https://github.com/austinyu
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@184276c81a2874427268f4fe0f3e2793d873c022 -
Trigger Event:
release
-
Statement type:
File details
Details for the file ujson5-1.0.1-py3-none-any.whl.
File metadata
- Download URL: ujson5-1.0.1-py3-none-any.whl
- Upload date:
- Size: 26.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ec8d686980f73ebc0b560b71055dc6444e31f52058f9cd468e75ba38e60fb92c
|
|
| MD5 |
fb78d3672b3a792991eacc72775c24ef
|
|
| BLAKE2b-256 |
c4b0617833346a32d6b83b0f5de04bd45f3da0d0f03e1383ed20d1cea7b8453c
|
Provenance
The following attestation bundles were made for ujson5-1.0.1-py3-none-any.whl:
Publisher:
release.yml on austinyu/ujson5
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ujson5-1.0.1-py3-none-any.whl -
Subject digest:
ec8d686980f73ebc0b560b71055dc6444e31f52058f9cd468e75ba38e60fb92c - Sigstore transparency entry: 184709449
- Sigstore integration time:
-
Permalink:
austinyu/ujson5@184276c81a2874427268f4fe0f3e2793d873c022 -
Branch / Tag:
refs/tags/v1.0.1 - Owner: https://github.com/austinyu
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@184276c81a2874427268f4fe0f3e2793d873c022 -
Trigger Event:
release
-
Statement type: