First install and import 'json' module
import json
Reading JSON object from file:
def read_json_from_file(json_file): """ Read array of objects from a file in json format. :param json_file: input json file name :return: array of objects from the json """ with open(json_file, 'r') as f: exp_string = json.load(f) json_array = json.loads(exp_string) return json_array
Writing JSON to the file:
def write_json_to_file(array, out_file_name): """ Dump an array to file in json format. :param array: Array to write to the file :param out_file_name: output file name """ with open(out_file_name, 'w') as out_f: s = json.dumps([o.__dict__ for o in array], sort_keys=True) json.dump(s, out_f)
No comments:
Post a Comment