How to Work with JSON in Python
Learn how to read, write, and manipulate JSON data in Python using the json module.
JSON (JavaScript Object Notation) is a common data format for APIs and configuration files. Python’s built-in json module makes working with JSON easy.
Importing the JSON Module
import json
Converting Python to JSON (Serialization)
json.dumps() - Python to JSON String
import json
# Dictionary to JSON string
data = {
"name": "Alice",
"age": 30,
"city": "New York",
"active": True,
"skills": ["Python", "SQL", "JavaScript"]
}
json_string = json.dumps(data)
print(json_string)
# {"name": "Alice", "age": 30, "city": "New York", "active": true, "skills": ["Python", "SQL", "JavaScript"]}
# Pretty print with indentation
json_pretty = json.dumps(data, indent=2)
print(json_pretty)
json.dump() - Write to File
import json
data = {"name": "Alice", "age": 30}
# Write to file
with open("data.json", "w") as f:
json.dump(data, f, indent=2)
Converting JSON to Python (Deserialization)
json.loads() - JSON String to Python
import json
json_string = '{"name": "Alice", "age": 30, "active": true}'
# Parse JSON string
data = json.loads(json_string)
print(data["name"]) # Alice
print(data["age"]) # 30
print(type(data)) # <class 'dict'>
json.load() - Read from File
import json
# Read from file
with open("data.json", "r") as f:
data = json.load(f)
print(data)
Type Conversions
Python to JSON type mappings:
| Python | JSON |
|---|---|
| dict | object |
| list, tuple | array |
| str | string |
| int, float | number |
| True | true |
| False | false |
| None | null |
import json
python_data = {
"string": "hello",
"number": 42,
"float": 3.14,
"boolean": True,
"null_value": None,
"list": [1, 2, 3],
"nested": {"key": "value"}
}
json_string = json.dumps(python_data, indent=2)
print(json_string)
Working with APIs
import json
import urllib.request
# Fetch JSON from API
url = "https://api.example.com/users/1"
with urllib.request.urlopen(url) as response:
data = json.loads(response.read().decode())
print(data["name"])
# Using requests library (recommended)
import requests
response = requests.get("https://api.example.com/users/1")
data = response.json() # Built-in JSON parsing
print(data["name"])
Handling Errors
import json
# Handle invalid JSON
invalid_json = "{'name': 'Alice'}" # Single quotes are invalid
try:
data = json.loads(invalid_json)
except json.JSONDecodeError as e:
print(f"Invalid JSON: {e}")
# Check if file exists
import os
if os.path.exists("data.json"):
with open("data.json") as f:
data = json.load(f)
else:
data = {} # Default value
Custom Serialization
Handling Non-Serializable Types
import json
from datetime import datetime
data = {
"name": "Event",
"date": datetime.now() # Not JSON serializable
}
# This will raise TypeError:
# json.dumps(data)
# Solution 1: Custom encoder
class DateTimeEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, datetime):
return obj.isoformat()
return super().default(obj)
json_string = json.dumps(data, cls=DateTimeEncoder)
print(json_string)
# Solution 2: Convert before serializing
data["date"] = data["date"].isoformat()
json_string = json.dumps(data)
Custom Object Serialization
import json
class User:
def __init__(self, name, email):
self.name = name
self.email = email
def to_dict(self):
return {"name": self.name, "email": self.email}
user = User("Alice", "alice@example.com")
# Convert to dict first
json_string = json.dumps(user.to_dict())
print(json_string)
# Or use __dict__
json_string = json.dumps(user.__dict__)
print(json_string)
Practical Examples
Configuration File
import json
# Load config
def load_config(filepath="config.json"):
try:
with open(filepath) as f:
return json.load(f)
except FileNotFoundError:
return {"debug": False, "port": 8000} # Defaults
# Save config
def save_config(config, filepath="config.json"):
with open(filepath, "w") as f:
json.dump(config, f, indent=2)
# Usage
config = load_config()
config["debug"] = True
save_config(config)
Data Processing
import json
# Process JSON array
json_data = '''
[
{"name": "Alice", "score": 85},
{"name": "Bob", "score": 92},
{"name": "Charlie", "score": 78}
]
'''
students = json.loads(json_data)
# Calculate average
average = sum(s["score"] for s in students) / len(students)
print(f"Average score: {average:.2f}")
# Filter and transform
passed = [s["name"] for s in students if s["score"] >= 80]
print(f"Passed: {passed}")
Summary
- Use
json.dumps()to convert Python to JSON string - Use
json.loads()to convert JSON string to Python - Use
json.dump()to write JSON to file - Use
json.load()to read JSON from file - Handle
JSONDecodeErrorfor invalid JSON - Use custom encoder for non-serializable types