I'm trying to create a function that would append data into json file follow with same indentation which is already exist. I created json file as given below.
{
"TableA":
[
{"ID": "10001", "Name": "Chandan","Age": "29"},
{"ID": "10002", "Name": "Rajesh", "Age": "24"},
{"ID": "10003", "Name": "Raju", "Age": "25"}
]
}
Python Code:
import json
# Write Data on Json file
a_dict = {"ID": "10005", "Name": "Manoj","Age": "31"}
try:
with open('TableA.json', 'a') as f:
json_obj = json.dump(a_dict, json.load(f),ensure_ascii=False)
f.write(json_obj)
f.close()
except IOError as io:
print "ERROR: ", io
# Read data from Json File
with open('TableA.json') as data_file:
data = json.load(data_file)
for i in data["TableA"]:
print "ID: \t", i["ID"]
print "Name: \t", i["Name"]
print "Age: \t", i["Age"]
python, (2) provide the relevant piece of code, and (3) describe the particular problem you bump into. Also provide an example with input and expected output.