Skip to content

Instantly share code, notes, and snippets.

@jonathanlurie
Last active November 10, 2021 13:39
Show Gist options
  • Save jonathanlurie/1b8d12f938b400e54c1ed8de21269b65 to your computer and use it in GitHub Desktop.
Save jonathanlurie/1b8d12f938b400e54c1ed8de21269b65 to your computer and use it in GitHub Desktop.
Serialize to JSON dictionaries containing Numpy types

NumpyTypeEncoder

Attempting to serialize into JSON a dictionary that contains Numpy-specific types such as np.ndarray, np.float64, np.uint32, etc. will generaly lead to error such as:

  • TypeError: Object of type ndarray is not JSON serializable
  • TypeError: Object of type float32 is not JSON serializable

To achieve a clean serialization, one must first cast all Numpy-type values into something more Python-friendly. This is done by extending the base JSONEncoder:

import json
import numpy as np


class NumpyTypeEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, np.generic):
            return obj.item()
        elif isinstance(obj, np.ndarray):
            return obj.tolist()
        return json.JSONEncoder.default(self, obj)


# Let's try it:
obj = {
    "some_float32": np.float32(10.),
    "some_float64": np.float32(20.),
    "some_int32": np.int32(30),
    "some_python_float": 40.,
    "a_numpy_array": np.array([1., 2., 3.]),
    "a_simple_list": [4., 5., 6.],
}

json_str = json.dumps(obj, indent=2, cls=NumpyTypeEncoder)
print(json_str)

And the output is:

{
  "some_float32": 10.0,
  "some_float64": 20.0,
  "some_int32": 30,
  "some_python_float": 40.0,
  "a_numpy_array": [
    1.0,
    2.0,
    3.0
  ],
  "a_simple_list": [
    4.0,
    5.0,
    6.0
  ]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment