Python json
Module: Detailed Overview and Examples
The json
module in Python provides an easy way to encode and decode JSON (JavaScript Object Notation) data. JSON is a popular data format used for transmitting data in web applications and is commonly used for configuration files. The json
module includes functions for serializing Python objects into JSON strings and deserializing JSON strings into Python objects.
Importing the json
Module
To use the functions from the json
module, you need to import it:
Key Functions and Methods
1. 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)
Serializes a Python object (obj
) into a JSON string.
Parameters
skipkeys
: IfTrue
, skip keys that are not basic types (str, int, float, bool, None).ensure_ascii
: IfTrue
, escape all non-ASCII characters.check_circular
: IfTrue
, check for circular references.allow_nan
: IfTrue
, allow NaN and Infinity values.cls
: An optional custom encoder class.indent
: If notNone
, pretty-print the JSON string with the specified number of spaces.separators
: A tuple specifying how to separate objects and arrays.default
: A function for custom serialization of objects that are not serializable by default.sort_keys
: IfTrue
, sort the keys in the output.
Example
import json
# Python object to be serialized
data = {
"name": "Alice",
"age": 30,
"city": "New York",
"hobbies": ["Reading", "Cycling"]
}
# Serialize Python object to JSON string
json_string = json.dumps(data, indent=4, sort_keys=True)
print(json_string)
Output:
2. 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)
Serializes a Python object (obj
) and writes it to a file-like object (fp
).
Example
import json
# Python object to be serialized
data = {
"name": "Alice",
"age": 30,
"city": "New York",
"hobbies": ["Reading", "Cycling"]
}
# Serialize Python object and write to a file
with open('data.json', 'w') as file:
json.dump(data, file, indent=4, sort_keys=True)
3. json.loads(s, *, encoding=None, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None)
Deserializes a JSON string (s
) into a Python object.
Parameters
encoding
: The encoding of the JSON string (not needed in Python 3).cls
: An optional custom decoder class.object_hook
: A function to convert parsed JSON objects into custom Python objects.parse_float
: A function to convert JSON floating-point numbers.parse_int
: A function to convert JSON integers.parse_constant
: A function to handle JSON constants (e.g., NaN).object_pairs_hook
: A function to handle pairs of key-value tuples.
Example
import json
# JSON string to be deserialized
json_string = '''
{
"name": "Alice",
"age": 30,
"city": "New York",
"hobbies": ["Reading", "Cycling"]
}
'''
# Deserialize JSON string to Python object
data = json.loads(json_string)
print(data)
Output:
4. json.load(fp, *, encoding=None, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None)
Deserializes a JSON object from a file-like object (fp
) into a Python object.
Example
import json
# Deserialize JSON from a file
with open('data.json', 'r') as file:
data = json.load(file)
print(data)
5. Handling Custom Serialization
You can use the default
parameter in json.dumps()
to handle objects that are not serializable by default.
Example
import json
from datetime import datetime
# Custom serialization function
def encode_datetime(obj):
if isinstance(obj, datetime):
return obj.isoformat()
raise TypeError("Type not serializable")
# Python object containing a datetime
data = {
"event": "Conference",
"date": datetime(2024, 8, 4)
}
# Serialize Python object to JSON string with custom serialization
json_string = json.dumps(data, default=encode_datetime, indent=4)
print(json_string)
Output:
6. Handling JSON Decoding Errors
You can handle errors that occur during JSON decoding using exception handling.
Example
import json
# Invalid JSON string
json_string = '{"name": "Alice", "age": 30, "city": "New York"'
try:
data = json.loads(json_string)
except json.JSONDecodeError as e:
print(f"Error decoding JSON: {e}")
Output:
Conclusion
The json
module is a powerful tool for working with JSON data in Python. It provides functions for both serializing Python objects to JSON strings and deserializing JSON strings into Python objects. Understanding how to use these functions, including handling custom serialization and managing errors, can greatly enhance your ability to work with data in a structured format.