Reading JSON
Converting JSON text (usually from the web) into a Python dictionary is called Deserialization or Parsing. We use json.loads() for this.
import json
# JSON text as a string
json_string = '{"name": "Sai", "age": 22, "is_student": true}'
# Parsing JSON string to Python Dictionary
data = json.loads(json_string)
print(type(data)) # Output: <class 'dict'>
print(data["name"]) # Output: Sai
print(data["is_student"]) # Output: True (automatically converted)
Reading from a JSON File:
If the JSON data is stored in a file, use json.load():
with open("user.json", "r") as file:
data = json.load(file)
print(data)
15.4 Writing JSON
Converting a Python dictionary into a JSON string is called Serialization. We use json.dumps() for this.
import json
student_dict = {
"name": "Ram",
"age": 25,
"pass": True
}
# Converting Python Dictionary to JSON String
json_data = json.dumps(student_dict, indent=4) # indent makes it look clean
print(json_data)
print(type(json_data)) # Output: <class 'str'>
Saving to a JSON File:
To save a Python dictionary directly into a file as JSON, use json.dump():
with open("student.json", "w") as file:
json.dump(student_dict, file, indent=4)