save array to json file python code example
Example 1: python save list to json
import json
with open('data.json', 'w') as f:
json.dump(data, f)
Example 2: save json file python
with open('data.txt', 'w') as outfile:
json.dump(data, outfile)
Example 3: write json dump to file python
import json
import numpy as np
class NpEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, np.integer):
return int(obj)
elif isinstance(obj, np.floating):
return float(obj)
elif isinstance(obj, np.ndarray):
return obj.tolist()
else:
return super(NpEncoder, self).default(obj)
# Your codes ....
json.dumps(data, cls=NpEncoder)