numpy – Python – TypeError: Object of type int64 is not JSON serializable

numpy – Python – TypeError: Object of type int64 is not JSON serializable

json does not recognize NumPy data types. Convert the number to a Python int before serializing the object:

count__c: int(store[count].iloc[i])

You can define your own encoder to solve this problem.

import json
import numpy as np

class NpEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, np.integer):
            return int(obj)
        if isinstance(obj, np.floating):
            return float(obj)
        if isinstance(obj, np.ndarray):
            return obj.tolist()
        return super(NpEncoder, self).default(obj)

# Your codes .... 
json.dumps(data, cls=NpEncoder)

numpy – Python – TypeError: Object of type int64 is not JSON serializable

Ill throw in my answer to the ring as a bit more stable version of @Jie Yangs excellent solution.

My solution

numpyencoder and its repository.

from numpyencoder import NumpyEncoder

numpy_data = np.array([0, 1, 2, 3])

with open(json_file, w) as file:
    json.dump(numpy_data, file, indent=4, sort_keys=True,
              separators=(, , : ), ensure_ascii=False,
              cls=NumpyEncoder)

The breakdown

If you dig into hmallens code in the numpyencoder/numpyencoder.py file youll see that its very similar to @Jie Yangs answer:


class NumpyEncoder(json.JSONEncoder):
     Custom encoder for numpy data types 
    def default(self, obj):
        if isinstance(obj, (np.int_, np.intc, np.intp, np.int8,
                            np.int16, np.int32, np.int64, np.uint8,
                            np.uint16, np.uint32, np.uint64)):

            return int(obj)

        elif isinstance(obj, (np.float_, np.float16, np.float32, np.float64)):
            return float(obj)

        elif isinstance(obj, (np.complex_, np.complex64, np.complex128)):
            return {real: obj.real, imag: obj.imag}

        elif isinstance(obj, (np.ndarray,)):
            return obj.tolist()

        elif isinstance(obj, (np.bool_)):
            return bool(obj)

        elif isinstance(obj, (np.void)): 
            return None

        return json.JSONEncoder.default(self, obj)

Leave a Reply

Your email address will not be published. Required fields are marked *