1import json
2
3data = {"key": "value"}
4
5with open('data.json', 'w') as jsonfile:
6 json.dump(data, jsonfile)
7
1import json
2import numpy as np
3
4class NpEncoder(json.JSONEncoder):
5 def default(self, obj):
6 if isinstance(obj, np.integer):
7 return int(obj)
8 elif isinstance(obj, np.floating):
9 return float(obj)
10 elif isinstance(obj, np.ndarray):
11 return obj.tolist()
12 else:
13 return super(NpEncoder, self).default(obj)
14
15# Your codes ....
16json.dumps(data, cls=NpEncoder)