Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving data to a json file using python

Tags:

python

I have JSON data stored in a file (Movies.json). i want to read the file and do some process and then save the output in new file

with open("Movies.json",'r') as f:
        movies = json.loads(f.read())
        // doing some process

How can i save the output?

Currently, I am trying this but it's not working:

json.dump(movies, f)

1 Answers

for output you need to use 'w' instead of 'r'

import json 
# Opening JSON file
f = open('movies.json')

# returns JSON object as
# a dictionary
movies = json.load(f)

# Iterating through the json
# list
for i in movies:
    print(i['id'])
 
# output 
with open("Movies.json", 'w') as f:
    json.dump(movies, f)
    
# Closing file
f.close()
like image 108
Akash Singh Avatar answered Oct 17 '25 09:10

Akash Singh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!