pyjson5

2.0.0 · active · verified Fri Apr 10

PyJSON5 is a high-performance JSON5 serializer and parser library for Python 3, implemented in Cython. It extends the standard JSON format with features like comments, unquoted keys, and trailing commas, making it ideal for human-editable configuration files. This library is actively maintained, with frequent updates and a current stable version of 2.0.0.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to load JSON5 data from a string using `pyjson5.loads` and dump Python objects back to a JSON5 string using `pyjson5.dumps`. It also shows reading from and writing to file-like objects using `pyjson5.load` and `pyjson5.dump` respectively. Note the use of `io.StringIO` for in-memory file operations.

import pyjson5
import io

json5_data = """
{
  // This is a comment
  name: 'Project Alpha',
  version: 1.0.0,
  features: [
    'comments',
    'trailing commas',
  ],
  config: { key: 'value', }, // Trailing comma
}
"""

# Load from a string
data = pyjson5.loads(json5_data)
print(f"Loaded data: {data}")

# Dump to a string
output_json5 = pyjson5.dumps(data, indent=2)
print(f"\nDumped JSON5:\n{output_json5}")

# Load from a file-like object (StringIO acts like a file)
file_like_obj_read = io.StringIO(json5_data)
file_data = pyjson5.load(file_like_obj_read)
print(f"\nLoaded from file-like object: {file_data}")

# Dump to a file-like object
file_like_obj_write = io.StringIO()
pyjson5.dump(data, file_like_obj_write, indent=2)
print(f"\nDumped to file-like object:\n{file_like_obj_write.getvalue()}")

view raw JSON →