I noticed this problem when a computer running Ubuntu was updated recently and the default version of Python changed to 2.7.
import json
import numpy as np
Because the elements of a NumPy array are not native ints, but of NUmPy's own types:
>>> type(np.arange(5)[0])
You can use a custom JSONEncoder to support the ndarray type returned by arange:
import numpy as np
import json
class NumPyArangeEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, np.ndarray):
return obj.tolist() # or map(int, obj)
return json.JSONEncoder.default(self, obj)
print(json.dumps(np.arange(5), cls=NumPyArangeEncoder))