demjson3

3.0.6 · active · verified Wed Apr 15

demjson3 is a Python 3 specific library for encoding, decoding, and validating JSON data, fully compliant with RFC 7159. It is a fork of the `demjson` project, updated to address Python 3 compatibility issues and provide enhanced error handling and linting capabilities. It offers both strict JSON parsing and a non-strict mode for more JavaScript-like syntax. The library is currently at version 3.0.6, with its last release in October 2022.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates basic encoding and decoding of JSON data using `demjson3`. It shows how to convert Python dictionaries and lists to JSON strings and vice-versa. It also includes an example of using the `strict=False` option to parse more lenient, JavaScript-like JSON with features not allowed by RFC 7159.

import demjson3

# Encode Python data to JSON string
python_data = {'name': 'Alice', 'age': 30, 'isStudent': False, 'courses': ['Math', 'Science'], 'extra': None}
json_output = demjson3.encode(python_data, indent=2, strict=True)
print('Encoded JSON:')
print(json_output)

# Decode JSON string to Python data
json_input = '{"product": "Laptop", "price": 1200.50, "inStock": true}'
python_decoded = demjson3.decode(json_input)
print('\nDecoded Python data:')
print(python_decoded)

# Example of non-strict decoding (allowing comments and unquoted keys)
non_strict_json = "{ /* A comment */ myKey: 'value', another: undefined }"
python_non_strict = demjson3.decode(non_strict_json, strict=False)
print('\nDecoded non-strict JSON:')
print(python_non_strict)

view raw JSON →