Skip to main content

A fast JSON5 encoder/decoder for Python

Project description

ujson5

Documentation

Feature Comparison

CI/CD CI
build docs
Coverage / Codspeed codecov
CodSpeed Badge
Package PyPI - Version
PyPI - Python Version
PyPI - License
PyPI - Downloads
Meta pre-commit
Code style: black
Checked with mypy

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 json module in Python, you already know how to use ujson5. ujson5 API is almost identical to the json module with some additional features.
  • Robust test suite - ujson5 is tested against the most famous JSON5 test suite to ensure compatibility. See the testing section for more information.
  • Speed - ujson5 tokenizer and parser implement DFA-based algorithms for fast parsing, which is only slightly slower than the built-in json module.
  • Pythonic - Comments in python are directly encoded into JSON5 comments. Magic!
  • Quality code base - ujson5 is linted with flake8, formatted with black, and type-checked with mypy. What's more? 100% test coverage with pytest and codecov!
  • Friendly Error Messages - ujson5 provides detailed error messages to help you debug your JSON5 files, including the exact location of the error.
  • Type hints - ujson5 provides 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 from sys.stdin.

  • outfile: The JSON file to output the converted JSON. If not specified, output to sys.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.0a (2025-03-07)

GitHub release

What's Changed

New Contributors

Full Changelog: https://github.com/austinyu/ujson5/compare/v1.0.0...v1.0.0a

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ujson5-1.0.0b0.tar.gz (750.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ujson5-1.0.0b0-py3-none-any.whl (26.3 kB view details)

Uploaded Python 3

File details

Details for the file ujson5-1.0.0b0.tar.gz.

File metadata

  • Download URL: ujson5-1.0.0b0.tar.gz
  • Upload date:
  • Size: 750.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for ujson5-1.0.0b0.tar.gz
Algorithm Hash digest
SHA256 47a561770ed5b7bdb5f98418138117dc21a9c58180ea80bfbd1d2e71fdebc2e1
MD5 8133379dd6b99981e8ba891386892136
BLAKE2b-256 a926fb7d8450f9b033bf8e46cb0ae1302b1a2447f9652572227ec44e5ca64ec1

See more details on using hashes here.

Provenance

The following attestation bundles were made for ujson5-1.0.0b0.tar.gz:

Publisher: release.yml on austinyu/ujson5

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ujson5-1.0.0b0-py3-none-any.whl.

File metadata

  • Download URL: ujson5-1.0.0b0-py3-none-any.whl
  • Upload date:
  • Size: 26.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for ujson5-1.0.0b0-py3-none-any.whl
Algorithm Hash digest
SHA256 c57168ad39d37ee80e501c4938c918dbda5aee88aa77ebe4be60b77bacddf660
MD5 344ff3f0d23e51a28a8f9e5b4ce77651
BLAKE2b-256 76ace8d3b7caf6e36e4e447269c0ba6d9c04b0a6995d8d41e2dcfe060e9886d3

See more details on using hashes here.

Provenance

The following attestation bundles were made for ujson5-1.0.0b0-py3-none-any.whl:

Publisher: release.yml on austinyu/ujson5

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page