Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update json file

Tags:

python

json

I have json file with some data, and would like to occasionally update this file.

I read the file:

with open('index.json', 'rb') as f:
    idx = json.load(f)

then check for presence of a key from potentially new data, and if key is not present update the file:

with open('index.json', mode='a+') as f:
    json.dump(new_data, f, indent=4)

However this procedure just creates new json object (python dict) and appends it as new object in output json file, making the file not valid json file.

Is there any simple way to append new data to json file without overwriting whole file, by updating the initial dict?

like image 470
theta Avatar asked Mar 14 '13 17:03

theta


People also ask

How do I edit a JSON file?

Procedure. In the Enterprise Explorer view, right-click your . json file or other file type that contains JSON code and select Open With > JSON Editor. You can compress JSON strings so that the strings display on one line with white space removed between JSON elements.

Can we update JSON?

You can use Oracle SQL function json_mergepatch to update specific portions of a JSON document. You pass it a JSON Merge Patch document, which specifies the changes to make to a specified JSON document.

How do I update a JSON file in Python?

Updating a JSON object in Python is as simple as using the built-in update() function from the json package we have imported. The update method is used to add a new key-value pair to the JSON string that we declared in our code.


Video Answer


1 Answers

One way to do what you're after is to write one JSON object per line in the file. I'm using that approach and it works quite well.

A nice benefit is that you can read the file more efficiently (memory-wise) because you can read one line at a time. If you need all of them, there's no problem with assembling a list in Python, but if you don't you're operating much faster and you can also append.

So to initially write all your objects, you'd do something like this:

with open(json_file_path, "w") as json_file:
    for data in data_iterable:
        json_file.write("{}\n".format(json.dumps(data)))

Then to read efficiently (will consume little memory, no matter the file size):

with open(json_file_path, "r") as json_file:
    for line in json_file:
        data = json.loads(line)
        process_data(data)

To update/append:

with open(json_file_path, "a") as json_file:
    json_file.write("{}\n".format(json.dumps(new_data)))

Hope this helps :)

like image 176
kgr Avatar answered Oct 05 '22 07:10

kgr