Simplejson
Simplejson is a fast, extensible, and standards-compliant JSON encoder/decoder for Python. It's often used as a drop-in replacement for Python's built-in `json` module, offering a C extension for improved performance and additional features. The library is actively maintained, with the current stable version being 3.20.2, and releases occur several times a year.
Warnings
- breaking In versions 3.19.1 and later, security hardening measures changed default behavior to be more strict, consuming and producing only compliant JSON by default. If your application previously relied on non-compliant JSON features being tolerated (e.g., non-string keys if `use_decimal=True`), you might need to adjust your data or explicitly re-enable older, less strict behaviors using specific flags.
- gotcha simplejson disables C speedups on specific Python runtimes (e.g., PyPy, GraalPy) where they are not compatible or provide no benefit. If you are using these runtimes, expect equivalent performance to the pure-Python implementation.
- gotcha Versions 3.20.0 and 3.20.1 changed key memoization behavior to ensure keys are coerced to strings before memoization. While unlikely to cause direct breaks, if you had highly specific reliance on the internal memoization of non-string keys (e.g., custom objects used as keys that were not string-coerced until later stages), this might subtly alter behavior or performance profiles.
Install
-
pip install simplejson
Imports
- simplejson
import simplejson
- loads
simplejson.loads
- dumps
simplejson.dumps
Quickstart
import simplejson
data = {'name': 'Alice', 'age': 30, 'is_student': False, 'courses': ['Math', 'Science']}
# Encode Python object to JSON string
json_string = simplejson.dumps(data, indent=4)
print('Encoded JSON:')
print(json_string)
# Decode JSON string to Python object
decoded_data = simplejson.loads(json_string)
print('\nDecoded Data:')
print(decoded_data)
print(f"Accessing a value: {decoded_data['name']}")