Serialization and deserialization of objects
在真实开发中,我们几乎不会把数据存成零散的变量名。当你需要保存一份 AI 模型的超参数配置、向前端响应 JSON 数据,甚至让 Python 与 Java、Go 等异构系统通信时,都会面临同一个问题:**如何用一种通用的格式,无损地交换复杂结构的数据? **
The answer is JSON - currently the most popular "data bridge". This article uses the most direct way to take you to master the serialization and deserialization of JSON in Python, as well as two practical tools that allow you to get twice the result with half the effort.
1. What is JSON: From JS syntax sugar to “data passport”
The full name of JSON is JavaScript Object Notation. It was originally just a simple syntax for writing objects in JavaScript. However, due to its plain text, compact structure, and full cross-language support, it quickly replaced lengthy XML and became the de facto standard for API docking and configuration files.
JS ↔ Python data seamless mapping table
The structure of JSON is naturally similar to Python dictionaries and lists, and supports unlimited levels of nesting. However, for cross-language compatibility, JSON has fewer data types than Python. The corresponding relationship is shown in the table below:
Simply put, JSON is a "lite and universal version" of the Python container type. By mastering this table, you can easily complete the conversion between memory objects and JSON text.
2. Core tools: Python built-injsonmodule
Python comes withjsonThe module provides the bidirectional conversion capability of memory object ↔ JSON (string/file). There are only four core functions. Remember their suffixes to distinguish their uses:
Memory Tips: Bring
sis to process string (String) withoutsDirect manipulation of file objects.
3. Practical demonstration: serialization and deserialization
The following uses a complex dictionary with nested structures, Chinese, Boolean values, lists and empty fields to practice the complete access process. This example simulates the model configuration of an AI platform (Daoman).
3.1 Serialization: turning objects into JSON
Run this code and you will see a clearly formatted JSON string, anddaoman_lite_config.jsonThe file has also been written correctly.
3.2 Deserialization: Restore JSON to objects
Next, read back the configuration you just saved, and demonstrate how to restore data from a JSON string.
It can be seen that whether it is a file or a string,json.loadandjson.loadsIt can perfectly restore JSON data to Python's native dictionaries and lists, and the numerical types are automatically matched.
4. Extra meal 1: Performance optimization under massive data
When it comes to processing more than one million JSON data (such as product information captured by crawlers and batch AI inference results), Python's built-injsonModules can become a bottleneck. At this time, you can use ujson - a third-party library called "Lightning Parser", which is usually 3 to 10 times faster than the built-in version.
4.1 pip configure domestic mirror (speed up download)
pipIt is the official package management tool of Python. It is downloaded from foreign PyPI by default. Domestic access is often very slow. It is recommended to configure the Tsinghua image first:
4.2 Install and use ujson
After installing ujson, you only need to modify one line of import statements, and the rest of the code is fully compatible with the built-injsonThe four core functions of the module.
pip Common Command Cheat Sheet
5. Extra meal 2: Actual call to the public JSON API
Most APIs today return JSON data over HTTP/HTTPS. Python built-inurllibIt is more cumbersome to use, and it is recommended to use the requests library - it is called "the most elegant HTTP library" and has built-in JSON parsing methods.
5.1 Install requests
5.2 Calling News API (Example)
The following takes the free domestic news API of Tianju Shuxing as an example (you need to apply for a free key by yourself, and the daily call volume is 100 times). in the codeYOUR_API_KEYJust replace it with your own key.
resp.json()Behind this call, it actually executes the response content.json.loads(), which saves us the step of manual analysis, which is very convenient.
6. Summary
Today we only focus on three things, but they are enough to cover 90% of JSON scenarios in daily development:
- Positioning of JSON: a cross-language, plain text, compact “data bridge”
- Python’s four built-in json modules:
dumps/dump(serialization),loads/load(deserialization) - Two efficiency-improving gadgets:
- ujson: Make processing large JSON lightning fast
- requests: Call JSON API gracefully
Finally, I would like to remind you of the two most common mistakes:
- When writing a JSON file must be specified
encoding='utf-8', otherwise the Chinese characters will be garbled. - JSON keys must be double quoted strings. Although Python's single-quote keys are automatically converted during serialization, when manually spelling a JSON string, you should never write out the single quotes.
Master these, and you can confidently use JSON to store, transmit, and exchange data in a variety of projects.

