json-numpy
raw JSON → 2.1.1 verified Mon Apr 27 auth: no python
A library for encoding and decoding NumPy arrays and scalars to/from JSON-compatible formats. Current version 2.1.1, compatible with Python >=3.8. Maintained actively.
pip install json-numpy Common errors
error AttributeError: module 'json_numpy' has no attribute 'dumps' ↓
cause Incorrect import assuming dumps/loads are top-level; they are inside json_numpy module.
fix
Use json_numpy.dumps() and json_numpy.loads() after importing json_numpy.
error TypeError: Object of type ndarray is not JSON serializable ↓
cause Using standard json.dumps directly on numpy arrays.
fix
Use json_numpy.dumps(arr) instead of json.dumps(arr).
Warnings
gotcha json_numpy.dumps does not encode Infinity or NaN by default; they become null. To preserve them, pass allow_nan=True. ↓
fix Use json_numpy.dumps(obj, allow_nan=True).
gotcha Deserialization returns numpy arrays by default. To get Python lists, convert manually. ↓
fix Use json_numpy.loads(s, as_list=True) or explicit list() after decode.
Imports
- json_numpy
import json_numpy
Quickstart
import json_numpy
import numpy as np
arr = np.array([1, 2, 3])
encoded = json_numpy.dumps(arr)
print(encoded)
decoded = json_numpy.loads(encoded)
print(decoded)