dump in json python code example

Example 1: python json dump to file

import json
with open('data.json', 'w') as f:
    json.dump(data, f)

Example 2: python json dump

import json

with open("data_file.json", "w") as write_file:
    json.dump(data, write_file)

Example 3: json load

import json

with open('path_to_file/person.json') as f:
  data = json.load(f)

Example 4: json load

import json

with open('path_to_file/person.json') as f:
  data = json.load(f)

# Output: {'name': 'Bob', 'languages': ['English', 'Fench']}
print(data)

Example 5: 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)