Master Python Serialization: Pickle, JSON, and XML with Real-World API Examples
This guide explains how to serialize and deserialize Python objects using the pickle, json, and xml.etree.ElementTree modules, and demonstrates practical API testing scenarios—including POST, GET, PUT, DELETE, and nested JSON handling—so you can choose the right format for your needs.
Serialization converts a data structure or object state into a storable or transmittable format, while deserialization restores it. In Python, the built‑in pickle, json, and xml.etree.ElementTree modules cover the most common use cases.
Using the pickle Module
Serialization
import pickle
data = {
'name': 'Alice',
'age': 30,
'scores': [85, 92, 78]
}
# Serialize to binary and write to a file
with open('data.pickle', 'wb') as file:
pickle.dump(data, file)Deserialization
import pickle
# Read the binary file and restore the object
with open('data.pickle', 'rb') as file:
loaded_data = pickle.load(file)
print(loaded_data)Using the json Module
Serialization
import json
data = {
'name': 'Alice',
'age': 30,
'scores': [85, 92, 78]
}
# Convert to a JSON string
json_string = json.dumps(data)
print(json_string)
# Or write directly to a file
with open('data.json', 'w') as file:
json.dump(data, file)Deserialization
import json
# From a JSON string
data = json.loads(json_string)
print(data)
# From a file
with open('data.json', 'r') as file:
loaded_data = json.load(file)
print(loaded_data)Using xml.etree.ElementTree
Serialization
import xml.etree.ElementTree as ET
data = {
'name': 'Alice',
'age': 30,
'scores': [85, 92, 78]
}
# Create root element
root = ET.Element('person')
ET.SubElement(root, 'name').text = data['name']
ET.SubElement(root, 'age').text = str(data['age'])
scores_element = ET.SubElement(root, 'scores')
for score in data['scores']:
ET.SubElement(scores_element, 'score').text = str(score)
# Convert the tree to a string
xml_string = ET.tostring(root, encoding='unicode')
print(xml_string)
# Or write to a file
tree = ET.ElementTree(root)
tree.write('data.xml')Deserialization
import xml.etree.ElementTree as ET
# From an XML string
root = ET.fromstring(xml_string)
# Parse the data
data = {
'name': root.find('name').text,
'age': int(root.find('age').text),
'scores': [int(score.text) for score in root.find('scores').findall('score')]
}
print(data)Serialization in API Automation Testing
When testing RESTful APIs, JSON is the de‑facto data exchange format. The following scenarios illustrate how to serialize Python objects to JSON, send HTTP requests with requests, and deserialize the responses.
Scenario 1 – POST a New User
import requests, json
user_data = {
"username": "testuser",
"email": "[email protected]",
"password": "securepassword"
}
json_data = json.dumps(user_data)
response = requests.post('https://api.example.com/users', data=json_data, headers={"Content-Type": "application/json"})
assert response.status_code == 201
response_data = response.json()
print(response_data)Scenario 2 – GET and Verify User Info
import requests
response = requests.get('https://api.example.com/users/1')
assert response.status_code == 200
user_info = response.json()
expected_user_info = {
"id": 1,
"username": "testuser",
"email": "[email protected]"
}
assert user_info == expected_user_info
print(user_info)Scenario 3 – UPDATE User Email
import requests, json
update_data = {"email": "[email protected]"}
json_data = json.dumps(update_data)
response = requests.put('https://api.example.com/users/1', data=json_data, headers={"Content-Type": "application/json"})
assert response.status_code == 200
updated_user_info = response.json()
expected_updated_user_info = {
"id": 1,
"username": "testuser",
"email": "[email protected]"
}
assert updated_user_info == expected_updated_user_info
print(updated_user_info)Scenario 4 – DELETE a User
import requests
response = requests.delete('https://api.example.com/users/1')
assert response.status_code == 204
# Verify deletion
get_response = requests.get('https://api.example.com/users/1')
assert get_response.status_code == 404
print('User has been successfully deleted.')Scenario 5 – Handle Nested JSON (Order Details)
import requests, json
response = requests.get('https://api.example.com/orders/1')
assert response.status_code == 200
order_info = response.json()
expected_order_info = {
"id": 1,
"customer": {
"id": 1,
"name": "John Doe",
"email": "[email protected]"
},
"items": [
{"product_id": 1, "quantity": 2},
{"product_id": 2, "quantity": 1}
],
"total_amount": 150.00
}
assert order_info == expected_order_info
print(order_info)Conclusion
Pickle is convenient for persisting Python‑specific objects but is unsuitable for cross‑language or network transmission. JSON offers language‑agnostic, lightweight exchange ideal for simple structures and API communication. XML provides a more verbose, hierarchical format useful when detailed schema validation is required. Choose the serialization method that aligns with your project's language compatibility, data complexity, and performance constraints.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
