I'm new to Json and Python and have been trying to update a Json file, I'm struggling with the process to read, update and save the file with the old and new information. I try it using only Json Strings but had a lot of problems with the format and I prefer to read the Json to a Pandas Dataframe, update and then save it. I am able to save and update the dataframe, but have problems reading the file to the Dataframe.
Everything is working fine except the read_json function:
df = df.read_json("registryDB.json")
I am getting this error:
AttributeError: 'DataFrame' object has no attribute 'read_json'
This is the function code:
df = df.read_json("registryDB.json")
df = df.append({
'Name': 'John',
'User': 'John123',
'Last Name': 'Doe',
'Age': 27,
'Gender': 'm',
'Location': 'US',
'Date': timestamp
}, ignore_index=True)
file = df.to_json(orient='table')
with open("registryDB.json", "w") as dataFile:
json.dump(file, dataFile)
I don't know if this is the best or correct way of doing this, so if you know any other, any advise would be awesome.
Thank you!
jsonmodule for writing back to the file. I'd suggest to use it also for editing the data and skip usingpandasentirely. See my answer below for more info and also for a quick note on where your error comes from.