json.get python code example
Example 1: python json string to object
import json
x = '{ "name":"John", "age":30, "city":"New York"}'
y = json.loads(x)
print(y["age"])
Example 2: load json
import json
with open('data.txt') as json_file:
data = json.load(json_file)
Example 3: 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)