Python JSON serialization basics¶
JSON (JavaScript Object Notation) is a fundamental standard for data structures in software engineering.^[400-devops__09-Scripting-Language__python__introduction__part-3.json__README.md] It is widely used for data storage and transfer, such as in HTTP Web APIs, application configuration files, and database storage.^[400-devops__09-Scripting-Language__python__introduction__part-3.json__README.md]
JSON Syntax¶
JSON structures data using specific syntax rules:^[400-devops__09-Scripting-Language__python__introduction__part-3.json__README.md]
- Objects: Defined by curly braces
{}. - Arrays (Lists): Defined by square brackets
[]. - Format: Keys and values are enclosed in double quotes, separated by a colon.
In Python, JSON objects typically map to dictionaries, and JSON arrays map to lists.
The json Library¶
Python includes a built-in json library for handling JSON data serialization and deserialization^[400-devops__09-Scripting-Language__python__introduction__part-3.json__README.md].
json.dumps(): Converts a Python object (typically a dictionary) into a JSON formatted string^[400-devops__09-Scripting-Language__python__introduction__part-3.json__README.md].json.loads(): Parses a JSON formatted string back into a Python dictionary^[400-devops__09-Scripting-Language__python__introduction__part-3.json__README.md].
Serialization Constraints¶
The json.dumps() function has specific requirements regarding the input data type^[400-devops__09-Scripting-Language__python__introduction__part-3.json__README.md]. It can serialize standard data types like strings and numbers, but it cannot directly serialize custom objects.^[400-devops__09-Scripting-Language__python__introduction__part-3.json__README.md]
If you attempt to serialize a custom class instance, it will raise a TypeError: Object of type ... is not JSON serializable^[400-devops__09-Scripting-Language__python__introduction__part-3.json__README.md]. To successfully serialize custom objects, they must first be converted into a dictionary format^[400-devops__09-Scripting-Language__python__introduction__part-3.json__README.md].
Working with Custom Objects¶
To serialize custom objects to JSON, the data must be transformed from a class instance to a standard dictionary^[400-devops__09-Scripting-Language__python__introduction__part-3.json__README.md]. A common method to achieve this in Python is using the __dict__ attribute^[400-devops__09-Scripting-Language__python__introduction__part-3.json__README.md].
For example, a Customer object can be converted into a dictionary like so:^[400-devops__09-Scripting-Language__python__introduction__part-3.json__README.md]
# Converting a list of objects to a dictionary structure for JSON
customerDict = {}
for id in customers:
customerDict[id] = customers[id].__dict__
jsonData = json.dumps(customerDict)
File I/O Operations¶
The typical workflow for persisting data involves reading from and writing to .json files^[400-devops__09-Scripting-Language__python__introduction__part-3.json__README.md].
Writing JSON:
import json
def updateCustomers(customer):
with open('customers.json', 'w', newline='') as customerFile:
customerJSON = json.dumps(customer)
customerFile.write(customerJSON)
Reading JSON:
import json
import os.path
def getCustomers():
if os.path.isfile("customers.json"):
with open('customers.json', newline='') as customerFile:
data = customerFile.read()
customers = json.loads(data)
return customers
else:
return {}
Related Concepts¶
- File I/O
- [[Dictionaries]]
- [[HTTP Web APIs]]