How to Parse and Dump JSON Files in Python?

Estimated read time 1 min read

To parse and dump JSON files in Python, you can use the built-in json module. Here’s an example:

import json

# Read JSON from a file
with open('input.json') as f:
    data = json.load(f)

# Modify the data
data['name'] = 'John'

# Write the modified JSON to a file
with open('output.json', 'w') as f:
    json.dump(data, f)

In this example, we first read the JSON data from a file called input.json using the json.load() method. We then modify the data by changing the value of the name key. Finally, we write the modified JSON data to a file called output.json using the json.dump() method.

Note that json.load() and json.dump() automatically handle encoding and decoding of JSON data. If you need more control over the encoding or decoding process, you can use the json.loads() and json.dumps() functions, respectively.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply