| name | json-parsing |
| description | Parse, validate, and serialize JSON data structures in Python. Use when working with JSON files, API payloads, or converting between JSON and Python objects. |
JSON Parsing
Handle JSON data in Python for file I/O, API communication, and data serialization.
Basic Operations
import json
# Parse JSON string
data = json.loads('{"key": "value", "count": 42}')
# Serialize to JSON string
json_str = json.dumps(data)
# Pretty printing
print(json.dumps(data, indent=2))
File I/O
# Read JSON file
with open('data.json', 'r') as f:
data = json.load(f)
# Write JSON file
with open('output.json', 'w') as f:
json.dump(data, f, indent=2)
Working with Complex Types
from decimal import Decimal
from datetime import datetime
# Custom encoder
class CustomEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, Decimal):
return float(obj)
if isinstance(obj, datetime):
return obj.isoformat()
return super().default(obj)
# Usage
data = {'price': Decimal('19.99'), 'timestamp': datetime.now()}
json_str = json.dumps(data, cls=CustomEncoder)
Safe Access Patterns
# Get with default
value = data.get('key', 'default_value')
# Nested access
email = data.get('user', {}).get('email', '')
# Check key existence
if 'key' in data:
process(data['key'])
Validation
# Validate JSON structure
def validate_json(json_str):
try:
data = json.loads(json_str)
return data
except json.JSONDecodeError as e:
print(f"Invalid JSON: {e}")
return None
# Validate schema
def validate_schema(data, required_keys):
return all(k in data for k in required_keys)
Array Handling
# Parse JSON array
records = json.loads('[{"id": 1}, {"id": 2}]')
# Process array
for record in records:
print(record['id'])
# JSON Lines format (one JSON per line)
with open('data.jsonl', 'r') as f:
for line in f:
record = json.loads(line.strip())
process(record)
Common Patterns
# Merge dictionaries
merged = {**dict1, **dict2}
# Filter keys
filtered = {k: v for k, v in data.items() if k in allowed_keys}
# Sort keys for consistent output
json.dumps(data, sort_keys=True)