Update JSON file in Python
the file will always be overwritten. But I want the items in statistics to be added. I tried reading the file in many possible ways and append it afterwards but it never worked.
The data is coming written in the information variable just like
Where is the new data coming from? Please show us your existing code so we can tell you how to fix it.
tried reading the file in many possible ways and append it afterwards but it never worked.. Please show that code
It is a file, not a database. Using a csv file, one could append new rows to the end. Amending JSON objects, however, requires amending the file in place and adjusting any needed brackets, commas or parenthesis. You are probably better off to read the file as JSON, modify your objects, and then use json.dump to overwrite the file with the new objects.
4 Answers 4
You would want to do something along the lines of:
def append_statistics(filepath, num_of_posts, followers, following): with open(filepath, 'r') as fp: information = json.load(fp) information["statistics"].append(< "date": datetime.now().strftime('%Y-%m-%d %H:%M:%S'), "num_of_posts": num_of_posts, "followers": followers, "following": following >) with open(filepath, 'w') as fp: json.dump(information, fp, indent=2)
Thanks 🙂 This actually helped me! I was finally able to read the file and modify it. Even though I think the JSON syntax is way too strict for this little project. That’s why I’m working with csv now which makes it way easier for me.
@lucanello Glad it helped. CSV sounds like a good idea for this task, as then you can just append to the end of the file instead of rewriting it every time.
You need to read the .json file and then append the new dataset and dump that data. See the code.
import json appending_statistics_data = <> appending_statistics_data["followers"] = 2346 appending_statistics_data["date"] = "2018-02-06 02:10:00" appending_statistics_data["num_of_posts"] = 234 appending_statistics_data["following"] = 234 with open(file.json, 'r') as fp: data = json.load(fp) data['statistics'].append(appending_statistics_data) #print(json.dumps(data,indent=4)) with open(file.json, 'w') as fp: json.dump(data, fp, indent=2)
How to Modify JSON File in Python?
In this example, you will learn python read and write to same json file. We will look at an example of append json file in python. We will use python open json file and modify. I would like to show you how to update json file in python. Here, Create a basic example of how to edit a json file in python.
There are several ways to update the JSON file in python. Here, i will give you very simple example of edit JSON file using open() , append() , dump() and close() functions. so let’s see a simple example below:
You can use these examples with python3 (Python 3) version.
I simply created data.json file with content as like below showed you. we will open that file and read it, Then write some more content on it.
import json # Read Existing JSON File with open('data.json') as f: data = json.load(f) # Append new object to list data data.append(< "ID": 4, "Name": "Paresh Patel", "email": "paresh@gmail.com" >) # Append new object to list data data.append(< "ID": 5, "Name": "Rakesh Patel", "email": "rakesh@gmail.com" >) # Create new JSON file with open('data.json', 'w') as f: json.dump(data, f, indent=2) # Closing file f.close()
After run successfully above example, you will see data.json file saved in your root path and file content will be as the below:
Hardik Savani
I’m a full-stack developer, entrepreneur and owner of Aatman Infotech. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Python Create JSON File from Dict Example
- Python Create JSON File from List Example
- How to Write a JSON File in Python?
- How to Read a JSON File in Python?
- How to Create a JSON File in Python?
- Python Read CSV File Without Header Example
- How to Read a CSV File in Python?
- Python Read Text File into List Example
- How to Read Text File Line by Line in Python?
- Python Create Text File in Specific Directory Example
- Python List Add Element at Beginning Example
- Python Create Zip Archive from Directory Example
how to edit a json file using python
json_data becomes a dictionary with the same structure as your JSON. To access ‘inner’ elements, you just have to navigate the dictionary structure.
with open('filename.json', 'r') as f: json_data = json.load(f) json_data['some_id'][0]['embed'] = 'Some string' # On this line you needed to add ['embed'][0] with open('filename.json', 'w') as f: json.dump(json_data, f, indent=2)
This is returning this error Traceback (most recent call last): File «p:\Web Hosting\ServerX\public\test.py», line 6, in
Then, I think you must be wrong about the structure of your JSON. Possibly you have JSON embedded in a string. In that case you would need: json_loads(json_data[‘some_id’])[’embed’] = ‘Some string’
I got the solution after a lot testing and searching.
with open('filename.json', 'r') as f: json_data = json.load(f) json_data['some_id'][0]['embed'] = 'Some string' with open('filename.json', 'w') as f: json.dump(json_data, f, indent=2)
It neither duplicate any data nor delete any existing data just change. OMG I got it finally.
If I understood it correctly, you want to edit the value on the file you already have (‘filename.json’), and not create a new file with that one field edited.
In that case, you don’t need the nested with open. structure. Also, notice you have to fully index the field you are trying to edit in the dictionary returned by the json.load() method. It will be under «some_id», but that key holds a list with one element, so then you need to refer to index0. Finally, the element on index0 is also a dictionary where the «embed» keys exist — and that’s what you want to change.
with open('filename.json', 'r+') as f: json_data = json.load(f) json_data['some_id'][0]['embed'] = 'Some string' json.dump(json_data, f, indent=2)
Update json file
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?
Is there a practical reason to not just rewrite the whole file? This sounds like it could get ugly. Plus, the underlying file doesn’t support «insert» operations, so if your update is near the beginning you will at least have to rewrite the rest of the file.
@theta: That’s not how it works; you updated the JSON structure by appending perhaps, but a file is not the same thing. You need to rewrite it.
1 Answer 1
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)
with open(json_file_path, "a") as json_file: json_file.write("<>\n".format(json.dumps(new_data)))