Have you ever struggled with JSON data processing? The four core functions of Python make it easy to handle data exchange!
In daily development, JSON is everywhere: API interfaces, configuration files, data storage… As a Python developer, mastering the four core functions of the json module is an essential skill. Today, we will delve into the secrets of these “four musketeers”!
1. Let’s get to know our “four musketeers”
Before diving into the details, let’s quickly understand these four core functions:
| Function | Role | Specialty |
|---|---|---|
<span>json.load()</span> |
File reading expert | Reads JSON data from a file |
<span>json.loads()</span> |
String parsing master | Parses JSON data from a string |
<span>json.dump()</span> |
File writing specialist | Writes data to a JSON file |
<span>json.dumps()</span> |
String generation master | Converts data to a JSON string |
These four brothers each have their strengths, and when used together, they can solve most JSON processing needs. Let’s explore them one by one!
2. json.load() – File Reading Expert
Function Signature
json.load(fp, *, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)
Parameter Details
1. <span>fp</span>(Required)
Purpose: A file object that must be readable and contain valid JSON content
Example:
import json
# Correct usage
with open('data.json', 'r', encoding='utf-8') as file:
data = json.load(file)
print(data)
# Incorrect usage: file not opened in read mode
file = open('data.json', 'w')
try:
data = json.load(file) # This will raise an error
except Exception as e:
print(f"Error: {e}")
2. <span>cls</span>(Optional)
Purpose: Specify a custom JSON decoder class
Example:
import json
from datetime import datetime
class CustomDecoder(json.JSONDecoder):
def decode(self, s, **kwargs):
obj = super().decode(s, **kwargs)
# Custom decoding logic
if 'create_time' in obj:
obj['create_time'] = datetime.fromisoformat(obj['create_time'])
return obj
with open('data_with_date.json', 'r') as file:
data = json.load(file, cls=CustomDecoder)
print(f"Creation time type: {type(data['create_time'])}")
3. <span>object_hook</span>(Optional)
Purpose: Perform custom processing on each decoded dictionary object
Example:
def datetime_hook(dct):
"""Convert specific fields to datetime objects"""
if 'timestamp' in dct:
dct['timestamp'] = datetime.fromisoformat(dct['timestamp'])
return dct
with open('data.json', 'r') as file:
data = json.load(file, object_hook=datetime_hook)
print(f"Timestamp type: {type(data['timestamp'])}")
4. <span>parse_float</span>/ <span>parse_int</span>/ <span>parse_constant</span>(Optional)
Purpose: Customize the parsing of floats, integers, and special constants
Example:
from decimal import Decimal
# Use Decimal instead of float for increased precision
with open('financial_data.json', 'r') as file:
data = json.load(file, parse_float=Decimal)
print(f"Amount data type: {type(data['amount'])}")
3. json.loads() – String Parsing Expert
Function Signature
json.loads(s, *, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)
Parameter Details
1. <span>s</span>(Required)
Purpose: A string containing a JSON document
Example:
import json
# Basic usage
json_string = '{"name": "Zhang San", "age": 30, "city": "Beijing"}'
data = json.loads(json_string)
print(data)
# Handling API response
import requests
response = requests.get('https://api.example.com/data')
if response.status_code == 200:
data = json.loads(response.text)
print(data)
2. <span>object_pairs_hook</span>(Optional)
Purpose: Use an ordered dictionary or custom object to maintain key order
Example:
from collections import OrderedDict
json_string = '{"z": 1, "a": 2, "m": 3}'
# Maintain key order
data = json.loads(json_string, object_pairs_hook=OrderedDict)
print("Ordered dict:", data)
print("Key order:", list(data.keys()))
# Compare with a normal dictionary
normal_data = json.loads(json_string)
print("Normal dict key order:", list(normal_data.keys()))
4. json.dump() – File Writing Expert
Function Signature
json.dump(obj, fp, *, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False, **kw)
Parameter Details
1. <span>obj</span>(Required)
Purpose: The Python object to serialize
2. <span>fp</span>(Required)
Purpose: A file object that must be writable
Example:
import json
data = {
"name": "Zhang San",
"age": 30,
"hobbies": ["Reading", "Swimming", "Programming"],
"address": {
"city": "Beijing",
"district": "Haidian"
}
}
with open('output.json', 'w', encoding='utf-8') as file:
json.dump(data, file, ensure_ascii=False, indent=2)
3. <span>ensure_ascii</span>(Optional, default True)
Purpose: Control the escaping of non-ASCII characters
Example:
chinese_data = {"name": "Zhang San", "city": "Beijing"}
# Default behavior: escape non-ASCII characters
with open('escaped.json', 'w') as f:
json.dump(chinese_data, f) # Chinese will be escaped
# Disable ASCII escaping
with open('raw.json', 'w', encoding='utf-8') as f:
json.dump(chinese_data, f, ensure_ascii=False) # Keep Chinese as is
4. <span>indent</span>(Optional, default None)
Purpose: Specify the indentation level for pretty output
Example:
data = {"a": 1, "b": [2, 3, 4], "c": {"d": 5}}
# No indentation (compact format)
with open('compact.json', 'w') as f:
json.dump(data, f) # {"a": 1, "b": [2, 3, 4], "c": {"d": 5}}
# Indent with 2 spaces
with open('pretty_2.json', 'w') as f:
json.dump(data, f, indent=2)
# Indent with 4 spaces
with open('pretty_4.json', 'w') as f:
json.dump(data, f, indent=4)
5. <span>sort_keys</span>(Optional, default False)
Purpose: Whether to sort the output by key name
Example:
data = {"zebra": 1, "apple": 2, "banana": 3}
# No sorting
with open('unsorted.json', 'w') as f:
json.dump(data, f, indent=2)
# Sort by key name
with open('sorted.json', 'w') as f:
json.dump(data, f, indent=2, sort_keys=True)
6. <span>separators</span>(Optional)
Purpose: Customize separators to optimize file size
Example:
data = {"name": "Zhang San", "age": 30, "city": "Beijing"}
# Default separators
with open('default.json', 'w') as f:
json.dump(data, f) # Uses (', ', ': ')
# Minimized separators (reduce file size)
with open('minified.json', 'w') as f:
json.dump(data, f, separators=(',', ':')) # Uses (',', ':')
7. <span>default</span>(Optional)
Purpose: Handle non-serializable objects
Example:
from datetime import datetime
class Product:
def __init__(self, id, name, price):
self.id = id
self.name = name
self.price = price
def custom_serializer(obj):
"""Custom serialization function"""
if isinstance(obj, datetime):
return obj.isoformat()
elif isinstance(obj, Product):
return {"id": obj.id, "name": obj.name, "price": obj.price}
raise TypeError(f"Cannot serialize type: {type(obj)}")
data = {
"timestamp": datetime.now(),
"product": Product("P1001", "Laptop", 5999.99)
}
with open('custom.json', 'w') as f:
json.dump(data, f, default=custom_serializer, indent=2)
5. json.dumps() – String Generation Expert
Function Signature
json.dumps(obj, *, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False, **kw)
Parameter Details
The parameters are essentially the same as <span>json.dump()</span><span>, but it returns a string instead of writing to a file.</span>
Example:
import json
data = {
"name": "Zhang San",
"age": 30,
"hobbies": ["Reading", "Swimming"]
}
# Generate JSON string
json_str = json.dumps(data, ensure_ascii=False, indent=2)
print("JSON string:")
print(json_str)
# For API response
from flask import jsonify
# In web frameworks, this is typically how to return a JSON response
def get_user_data():
user_data = {"name": "Zhang San", "age": 30}
return json.dumps(user_data, ensure_ascii=False), 200, {'Content-Type': 'application/json; charset=utf-8'}
6. Advanced Application Scenarios
1. Performance Optimization: Handling Large JSON Files
import json
import ijson
def stream_large_json(input_file, output_file):
"""Stream processing of large JSON files"""
with open(input_file, 'r', encoding='utf-8') as infile, \
open(output_file, 'w', encoding='utf-8') as outfile:
# Start writing JSON array
outfile.write('[\n')
first = True
# Use ijson for streaming parsing (for truly large files)
# or process line by line (for line-separated JSON)
for line in infile:
line = line.strip()
if line:
try:
data = json.loads(line)
# Process data...
processed = process_data(data)
# Write processed data
if not first:
outfile.write(',\n')
first = False
json.dump(processed, outfile, ensure_ascii=False)
except json.JSONDecodeError:
continue
outfile.write('\n]')
2. Custom Encoder/Decoder
import json
from datetime import datetime, date
from decimal import Decimal
class EnhancedJSONEncoder(json.JSONEncoder):
"""Enhanced JSON encoder that supports more data types"""
def default(self, obj):
if isinstance(obj, (datetime, date)):
return obj.isoformat()
elif isinstance(obj, Decimal):
return float(obj)
elif hasattr(obj, 'to_dict'):
return obj.to_dict()
return super().default(obj)
class EnhancedJSONDecoder(json.JSONDecoder):
"""Enhanced JSON decoder"""
def __init__(self, *args, **kwargs):
super().__init__(*args, object_hook=self.object_hook, **kwargs)
def object_hook(self, dct):
# Custom decoding logic
for key, value in dct.items():
if isinstance(value, str):
# Try to parse datetime
try:
dct[key] = datetime.fromisoformat(value)
except (ValueError, TypeError):
pass
return dct
# Using custom encoder
data = {
"timestamp": datetime.now(),
"price": Decimal('19.99'),
"product": Product("P1001", "Test", 100)
}
json_str = json.dumps(data, cls=EnhancedJSONEncoder, ensure_ascii=False, indent=2)
print("Custom encoding result:")
print(json_str)
# Using custom decoder
decoded = json.loads(json_str, cls=EnhancedJSONDecoder)
print(f"Decoded time type: {type(decoded['timestamp'])}")
7. Summary and Best Practices
Core Parameter Summary
| Parameter | Applicable Function | Purpose | Recommended Value |
|---|---|---|---|
<span>ensure_ascii</span> |
dump/dumps | Control escaping of non-ASCII characters | <span>False</span> (supports Chinese) |
<span>indent</span> |
dump/dumps | Output indentation | <span>2</span> or <span>4</span> (pretty output) |
<span>sort_keys</span> |
dump/dumps | Sort by key name | <span>True</span> (stable output) |
<span>separators</span> |
dump/dumps | Custom separators | <span>(', ', ': ')</span> (minimized) |
<span>default</span> |
dump/dumps | Handle non-serializable objects | Custom function |
<span>cls</span> |
All functions | Custom encoder/decoder | Custom class |
<span>object_hook</span> |
load/loads | Post-processing of objects | Custom function |
Best Practice Recommendations
- 1. Always specify encoding: Clearly specify
<span>encoding='utf-8'</span>when handling files - 2. Support for Chinese: Use
<span>ensure_ascii=False</span><span> to correctly handle Chinese characters</span> - 3. Error handling: Add appropriate exception handling
- 4. Performance considerations: Use streaming for large files
- 5. Data types: Use custom encoders for complex types
# Recommended standard usage
def safe_json_operations():
"""Safe JSON operation template"""
try:
# Read file
with open('data.json', 'r', encoding='utf-8') as f:
data = json.load(f, object_hook=custom_object_hook)
# Process data...
processed_data = process_data(data)
# Write file
with open('output.json', 'w', encoding='utf-8') as f:
json.dump(
processed_data,
f,
ensure_ascii=False,
indent=2,
default=custom_serializer
)
except FileNotFoundError:
print("File not found")
except json.JSONDecodeError as e:
print(f"JSON parsing error: {e}")
except TypeError as e:
print(f"Serialization error: {e}")
By deeply understanding these parameters and techniques, you will be able to handle various JSON data scenarios more flexibly and efficiently. Remember, choosing the right combination of parameters can greatly enhance the readability, performance, and maintainability of your code.
Practice is the best way to master this knowledge. Try using these advanced features in your existing projects and experience the convenience they bring!
Recommended Reading:
– Python JSON Handling: Four Tricks to Solve Data Exchange Problems!
– Python File Operation Secrets: Master These Methods to Easily Handle File Reading and Writing!
– Say Goodbye to Garbled Text! Python File Encoding Savior is Here
These are my personal insights; if you have other opinions, let’s discuss in the comments!!!
✨Follow me, to get more Python learning resources, practical projects, and industry trends! Reply “python learning” in the public account backend to get Python learning e-books!