JSON5 for Python

0.14.0 · active · verified Sat Mar 28

The `json5` library is a Python implementation of the JSON5 data format, which extends standard JSON to be more human-friendly, allowing features like JavaScript-style comments, unquoted object keys, trailing commas in objects and arrays, and single-quoted or multi-line strings. It aims to mirror the API of Python's built-in `json` module for ease of use. The current version is 0.14.0, and it maintains a regular release cadence.

Warnings

Install

Imports

Quickstart

Demonstrates parsing JSON5 strings using `json5.loads()` and serializing Python objects to JSON5 strings using `json5.dumps()`. The API closely resembles Python's standard `json` module.

import json5

# Parse a JSON5 string with comments and trailing commas
config_string = """
{
  // Application settings
  name: 'my-app',
  version: '1.0.0',
  features: [
    'comments',
    'trailing commas',
  ],
}
"""
config = json5.loads(config_string)
print(f"App Name: {config['name']}")

# Convert a Python object to a JSON5 string
data = {
  'name': 'another-app',
  'debug': True,
  'ports': [3000, 3001],
}
json5_string = json5.dumps(data, indent=2, quote_keys=False, trailing_commas=True)
print("\nSerialized JSON5:")
print(json5_string)

# Example of reading/writing a file (assuming a config.json5 exists)
# with open('config.json5', 'w') as f:
#     json5.dump(data, f, indent=2)
#
# with open('config.json5', 'r') as f:
#     loaded_config = json5.load(f)
# print(f"\nLoaded from file: {loaded_config['name']}")

view raw JSON →