best way to work with json files python code example
Example 1: python json file operations
import json
with open("jsonfile.json", "r") as jsonFile:
data = json.load(jsonFile)
'''
Example Json File:
[{"name": "Bill", "age": 20},
{"name": "Mary", "age": 31},
{"name": "Jake", "age": 19}]
'''
data[1]["age"] = 32
'''
Now the data variable is:
[{"name": "Bill", "age": 20},
{"name": "Mary", "age": 32},
{"name": "Jake", "age": 19}]
'''
with open("jsonfile.json", "w") as fileJson:
json.dump(data, fileJson)
Example 2: python import json data
import ast
def import_json(filename):
for line in open(filename):
yield ast.literal_eval(line)
data = list(import_json("/path/to/filename.json"))
dataframe = pd.DataFrame.from_dict(data)