Reading and Writing JSON in Python


Use the json module to work with JSON data.

Source Code

import json

# Writing JSON to a file
data = {"name": "Alice", "age": 26}
with open("data.json", "w") as f:
    json.dump(data, f)

# Reading JSON from a file
with open("data.json", "r") as f:
    loaded_data = json.load(f)
    print(loaded_data)
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments