1. Introduction to JSON
JSON is a lightweight data exchange format that is easy to read and write by people, and is also easy to analyze and generate by machines. It is based on a subset of JavaScript, but is language-independent and widely used in web applications, APIs, and configuration files.
JSON supports the following data types:
- Object (Dictionary)
- Array (list)
- String
- number
- Boolean value (true/false)
- null
2. Basic JSON operations in Python
Pythonjson
The module provides four main functions to handle JSON data:
2.1 () - Convert Python objects to JSON strings
import json data = { "name": "John Doe", "age": 30, "is_employee": True, "skills": ["Python", "JavaScript", "SQL"], "address": { "street": "123 Main St", "city": "New York" } } json_string = (data) print(json_string)
Output:
{"name": "John Doe", "age": 30, "is_employee": true, "skills": ["Python", "JavaScript", "SQL"], "address": {"street": "123 Main St", "city": "New York"}}
2.2 () - Convert JSON string to Python object
json_data = '{"name": "John Doe", "age": 30, "is_employee": true}' python_obj = (json_data) print(python_obj) print(type(python_obj))
Output:
{'name': 'John Doe', 'age': 30, 'is_employee': True} <class 'dict'>
2.3 () - Write Python objects to JSON files
with open('', 'w') as f: (data, f)
2.4 () - Read data from JSON file
with open('', 'r') as f: loaded_data = (f) print(loaded_data)
3. Advanced JSON processing
3.1 Beautify output (indent and sort)
pretty_json = (data, indent=4, sort_keys=True) print(pretty_json)
Output:
{ "address": { "city": "New York", "street": "123 Main St" }, "age": 30, "is_employee": true, "name": "John Doe", "skills": [ "Python", "JavaScript", "SQL" ] }
3.2 Custom encoder (handling non-JSON default types)
When you need to serialize types that are not supported by JSON by default (such as datetime objects), you can create a custom encoder:
from datetime import datetime import json class CustomEncoder(): def default(self, obj): if isinstance(obj, datetime): return () return super().default(obj) data = { "event": "Conference", "date": () } json_string = (data, cls=CustomEncoder) print(json_string)
3.3 Handling special floating point values
JSON standard does not support itNaN
、Infinity
and-Infinity
, but Python's json module can handle them:
data = { "temperature": float('nan'), "distance": float('inf') } json_string = (data, allow_nan=True) # The default is Trueprint(json_string)
3.4 Parsing large JSON files
For large JSON files, you can useijson
The library performs streaming to avoid memory problems:
import ijson with open('large_file.json', 'rb') as f: for prefix, event, value in (f): print(f"prefix: {prefix}, event: {event}, value: {value}")
4. Correlation between JSON and Python data types
JSON Type | Python types |
---|---|
object | dict |
array | list |
string | str |
number (int) | int |
number (real) | float |
true | True |
false | False |
null | None |
5. FAQs and Solutions
5.1 Date and time processing
JSON does not have a native date-time type, and usually has the following processing methods:
- Convert to ISO format string:
from datetime import datetime data = {"timestamp": ().isoformat()}
- Use timestamps:
data = {"timestamp": ().timestamp()}
5.2 Handling circular references
When there is a circular reference in the object, direct serialization will report an error:
a = {} b = {'a': a} a['b'] = b # This will throw TypeError: Circular reference detected# (a)
The solution is to break the loop reference or use a custom serializer.
5.3 Performance optimization
For large data structures:
- use
ujson
(UltraJSON)json
Module, faster - Consider using
orjson
(JSON library implemented by Rust) - For read-only operations, you can consider
simplejson
6. Best Practices
- Always handle exceptions:
try: data = (json_string) except as e: print(f"Invalid JSON: {e}")
Verify JSON data:
usejsonschema
The library verifies whether the JSON structure meets expectations.-
Safety considerations:
- Don't load JSON data from sources you don't trust
- Consider using
subclasses to limit the size and depth of parsed objects
-
API Design:
- Keep the JSON structure consistent
- Use meaningful field names
- Consider version control
Documented JSON structure:
Use OpenAPI/Swagger or JSON Schema to document your JSON interface.
7. Replace JSON's serialization format
While JSON is very popular, other formats may need to be considered in some scenarios:
- MessagePack: Binary format, smaller and faster
- YAML: More suitable for human reading and writing, support comments
- Protocol Buffers / Avro: Strong type, suitable for high-performance scenarios
- TOML: More suitable for configuration files
8. Summary
Pythonjson
Modules provide powerful and flexible JSON processing capabilities. Mastering JSON conversion is not only crucial for web development, but also a core skill in data processing and system integration. With the basic operations, advanced features and best practices described in this article, you should be able to use JSON efficiently and safely in your Python project.
Remember, choosing the right serialization format and tool can significantly impact the performance and maintainability of your application. In most cases, JSON is a good default option, but alternatives should also be considered based on specific needs.
The above is the detailed content of the comprehensive guide and best practices of JSON conversion in Python. For more information about Python JSON conversion, please follow my other related articles!