
Python
JSON (JavaScript Object Notation) is the standard format for exchanging data between web applications. Serialization converts Python objects to JSON, and deserialization converts JSON back to Python. This conversion is essential for working with APIs and web services.
JSON is a lightweight, text-based data format that's easy for both humans and computers to read and write. It's language-independent and is the de facto standard for web APIs.
# JSON looks like Python dictionaries and lists, but it's text
# JSON Example (as a string)
json_string = '''
{
"name": "Alice Johnson",
"age": 28,
"email": "alice@example.com",
"is_student": false,
"grades": [85, 90, 88],
"address": {
"street": "123 Main St",
"city": "San Francisco"
}
}
'''
# Python dict (similar but different)
python_dict = {
"name": "Alice Johnson",
"age": 28,
"email": "alice@example.com",
"is_student": False, # Python uses True/False
"grades": [85, 90, 88],
"address": {
"street": "123 Main St",
"city": "San Francisco"
}
}JSON supports these data types:
import json
# JSON data types and their Python equivalents
# String → String
json_string = '"Hello World"'
python_string = json.loads(json_string)
print(python_string) # Hello World
print(type(python_string)) # <class 'str'>
# Number → int or float
json_int = '42'
python_int = json.loads(json_int)
print(python_int, type(python_int)) # 42 <class 'int'>
json_float = '3.14'
python_float = json.loads(json_float)
print(python_float, type(python_float)) # 3.14 <class 'float'>
# Boolean → bool
json_bool_true = 'true'
json_bool_false = 'false'
print(json.loads(json_bool_true)) # True
print(json.loads(json_bool_false)) # False
# Null → None
json_null = 'null'
python_none = json.loads(json_null)
print(python_none) # None
# Array → list
json_array = '[1, 2, 3, "four", true]'
python_list = json.loads(json_array)
print(python_list) # [1, 2, 3, 'four', True]
# Object → dict
json_object = '{"key": "value", "number": 42}'
python_dict = json.loads(json_object)
print(python_dict) # {'key': 'value', 'number': 42}Converting Python objects to JSON is called serialization. Use `json.dumps()` to convert to string or `json.dump()` to write to file.
import json
# Python dict to JSON string
person = {
'name': 'Bob',
'age': 30,
'email': 'bob@example.com',
'hobbies': ['reading', 'gaming', 'coding']
}
# Convert to JSON string
json_string = json.dumps(person)
print(json_string)
# {"name": "Bob", "age": 30, "email": "bob@example.com", "hobbies": ["reading", "gaming", "coding"]}
# Compact format (no whitespace)
compact_json = json.dumps(person, separators=(',', ':'))
print(compact_json)
# Pretty format (with indentation)
pretty_json = json.dumps(person, indent=2)
print(pretty_json)
# {
# "name": "Bob",
# "age": 30,
# "email": "bob@example.com",
# "hobbies": [
# "reading",
# "gaming",
# "coding"
# ]
# }
# Sort keys alphabetically
sorted_json = json.dumps(person, sort_keys=True)
print(sorted_json)import json
person = {
'name': 'Carol',
'age': 25,
'city': 'New York'
}
# Write directly to file
with open('person.json', 'w') as f:
json.dump(person, f, indent=2)
# File now contains formatted JSONConverting JSON to Python objects is called deserialization. Use `json.loads()` for strings or `json.load()` for files.
import json
# JSON string
json_string = '''
{
"user_id": 123,
"username": "john_doe",
"is_active": true,
"scores": [95, 87, 92]
}
'''
# Convert to Python dict
data = json.loads(json_string)
# Access the data like normal Python dict
print(data['username']) # john_doe
print(data['scores'][0]) # 95
print(data['is_active']) # Trueimport json
# Read JSON from file
with open('person.json', 'r') as f:
person = json.load(f)
print(person['name']) # Carol
print(person['age']) # 25import json
from datetime import datetime
# This fails - datetime is not JSON serializable
data = {
'name': 'Alice',
'created_at': datetime.now() # ❌ TypeError!
}
# json.dumps(data) # Error!
# Solution 1: Convert to string first
data_fixed = {
'name': 'Alice',
'created_at': datetime.now().isoformat() # Convert to ISO string
}
json_string = json.dumps(data_fixed)
print(json_string)
# Solution 2: Use custom encoder
class DateTimeEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, datetime):
return obj.isoformat()
return super().default(obj)
data = {
'name': 'Alice',
'created_at': datetime.now()
}
json_string = json.dumps(data, cls=DateTimeEncoder)
print(json_string)import json
# JSON might be missing some fields
json_string = '{"name": "Dave"}'
data = json.loads(json_string)
# Safe access to missing keys
print(data.get('name')) # Dave
print(data.get('age')) # None
print(data.get('age', 25)) # 25 (default value)import json
# Complex nested structure
company_data = {
'name': 'Tech Corp',
'departments': [
{
'name': 'Engineering',
'employees': [
{'id': 1, 'name': 'Alice', 'skills': ['Python', 'JavaScript']},
{'id': 2, 'name': 'Bob', 'skills': ['Java', 'Go']}
]
},
{
'name': 'Sales',
'employees': [
{'id': 3, 'name': 'Carol', 'skills': ['Negotiation']}
]
}
]
}
# Serialize with pretty formatting
json_string = json.dumps(company_data, indent=2)
print(json_string)
# Parse back and access nested data
parsed = json.loads(json_string)
first_employee = parsed['departments'][0]['employees'][0]
print(f"{first_employee['name']}: {first_employee['skills']}")APIs typically return JSON. Here's how to handle it:
import requests
import json
# Get JSON from API
response = requests.get('https://jsonplaceholder.typicode.com/users/1')
# Method 1: Use .json() method (recommended)
user = response.json()
print(user['name'])
print(user['email'])
# Method 2: Manually parse JSON
user = json.loads(response.text)
print(user['name'])
# Send JSON data to API
new_post = {
'title': 'My Post',
'body': 'Post content',
'userId': 1
}
# Method 1: requests handles it
response = requests.post(
'https://jsonplaceholder.typicode.com/posts',
json=new_post # Automatically serializes
)
# Method 2: Manually serialize
json_string = json.dumps(new_post)
response = requests.post(
'https://jsonplaceholder.typicode.com/posts',
data=json_string,
headers={'Content-Type': 'application/json'}
)import json
# Create configuration
config = {
'app_name': 'MyApp',
'version': '1.0.0',
'debug': True,
'database': {
'host': 'localhost',
'port': 5432,
'name': 'myapp_db'
},
'api_keys': {
'github': 'token123',
'twitter': 'token456'
}
}
# Save configuration
with open('config.json', 'w') as f:
json.dump(config, f, indent=2)
# Load configuration
with open('config.json', 'r') as f:
loaded_config = json.load(f)
# Use configuration
print(f"App: {loaded_config['app_name']}")
print(f"DB: {loaded_config['database']['host']}")
print(f"Debug: {loaded_config['debug']}")| Task | Method | Example |
|---|---|---|
| Python → JSON String | `json.dumps()` | `json_str = json.dumps(dict)` |
| Python → JSON File | `json.dump()` | `json.dump(dict, file)` |
| JSON String → Python | `json.loads()` | `dict = json.loads(json_str)` |
| JSON File → Python | `json.load()` | `dict = json.load(file)` |
| Check type | `type()` | `type(value)` |
| Safe access | `.get()` | `dict.get('key', default)` |
| Pretty print | `indent` param | `json.dumps(data, indent=2)` |
Learn how to handle errors that occur when working with APIs.
Ready to practice? Try challenges or explore resources
Resources
Ojasa Mirai
Master AI-powered development skills through structured learning, real projects, and verified credentials. Whether you're upskilling your team or launching your career, we deliver the skills companies actually need.
Learn Deep • Build Real • Verify Skills • Launch Forward