Python JSON library (json module)¶
The Python json library is the standard module for parsing and serializing data in JSON (JavaScript Object Notation) format^[400-devops__09-Scripting-Language__python__introduction__part-3.json__README.md]. It allows Python applications to easily exchange data with web APIs, store configuration files, or manage data structures in a human-readable format^[400-devops__09-Scripting-Language__python__introduction__part-3.json__README.md].
Serialization and Deserialization¶
The library provides two primary functions for converting between Python dictionaries and JSON strings^[400-devops__09-Scripting-Language__python__introduction__part-3.json__README.md].
Serialization (json.dumps)¶
To store or transmit a Python dictionary, it must be converted into a JSON-formatted string. This is done using the json.dumps() function^[400-devops__09-Scripting-Language__python__introduction__part-3.json__README.md].
import json
customer_dict = {"id": 1, "name": "Alice"}
json_string = json.dumps(customer_dict)
Deserialization (json.loads)¶
To retrieve data from a JSON file or string and use it within a Python application, json.loads() is used to parse the raw data back into a dictionary^[400-devops__09-Scripting-Language__python__introduction__part-3.json__README.md].
import json
# Assuming 'data' is a string read from a file
with open('data.json', 'r') as f:
raw_data = f.read()
restored_dict = json.loads(raw_data)
Working with Objects¶
The json.dumps() function natively supports standard Python data types like dictionaries and lists^[400-devops__09-Scripting-Language__python__introduction__part-3.json__README.md]. However, passing custom class instances directly will result in a TypeError because these objects are not JSON serializable by default^[400-devops__09-Scripting-Language__python__introduction__part-3.json__README.md].
To serialize custom objects, they typically need to be converted into a dictionary representation first^[400-devops__09-Scripting-Language__python__introduction__part-3.json__README.md]. A common approach is to use the object's internal __dict__ attribute, which exposes the object's properties as a dictionary^[400-devops__09-Scripting-Language__python__introduction__part-3.json__README.md].
class Customer:
def __init__(self, id, name):
self.id = id
self.name = name
customer = Customer(1, "Bob")
# Convert object to dictionary before serialization
customer_json = json.dumps(customer.__dict__)
Data Storage Workflow¶
In application development, the JSON library is often used to persist application state. A typical workflow involves reading data from a .json file into a Python dictionary (deserialization), modifying the dictionary in memory, and then writing the updated dictionary back to the file (serialization)^[400-devops__09-Scripting-Language__python__introduction__part-3.json__README.md].
Related Concepts¶
- [[Python]]
- [[Serialization]]
- [[API Integration]]
- [[Data Structures]]
Sources¶
400-devops__09-Scripting-Language__python__introduction__part-3.json__README.md