0
0
Pythonprogramming~10 mins

Serializing and deserializing JSON in Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Serializing and deserializing JSON
Start with Python object
Serialize: Convert to JSON string
JSON string ready for storage or transfer
Deserialize: Convert JSON string back to Python object
Python object restored
End
This flow shows how a Python object is turned into a JSON string (serialization) and then back into a Python object (deserialization).
Execution Sample
Python
import json

person = {"name": "Anna", "age": 28}
json_str = json.dumps(person)
print(json_str)
restored = json.loads(json_str)
print(restored)
This code converts a Python dictionary to a JSON string and then back to a dictionary, printing both.
Execution Table
StepActionInputOutputNotes
1Define Python dict{"name": "Anna", "age": 28}person dictCreate a Python dictionary named person
2Serialize with json.dumpsperson dict"{\"name\": \"Anna\", \"age\": 28}"Convert dict to JSON string
3Print JSON string"{\"name\": \"Anna\", \"age\": 28}"{"name": "Anna", "age": 28}Output JSON string to console
4Deserialize with json.loads"{\"name\": \"Anna\", \"age\": 28}"restored dictConvert JSON string back to dict
5Print restored dictrestored dict{'name': 'Anna', 'age': 28}Output restored dict to console
6EndProgram finishes
💡 All steps complete, JSON string successfully serialized and deserialized.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 4Final
personundefined{"name": "Anna", "age": 28}{"name": "Anna", "age": 28}{"name": "Anna", "age": 28}{"name": "Anna", "age": 28}
json_strundefinedundefined"{\"name\": \"Anna\", \"age\": 28}""{\"name\": \"Anna\", \"age\": 28}""{\"name\": \"Anna\", \"age\": 28}"
restoredundefinedundefinedundefined{"name": "Anna", "age": 28}{"name": "Anna", "age": 28}
Key Moments - 3 Insights
Why does json.dumps output a string with escaped quotes?
json.dumps converts the Python dict into a JSON string format, which uses double quotes around keys and strings. The backslashes escape these quotes inside the Python string representation. See execution_table step 2.
Is the restored object after json.loads exactly the same as the original Python dict?
Yes, json.loads converts the JSON string back into a Python dictionary with the same keys and values. See execution_table steps 4 and 5.
Can we use json.loads directly on a Python dict?
No, json.loads expects a JSON string, not a Python dict. You must serialize first with json.dumps. See execution_table step 2 and 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what is the type of json_str?
AA JSON formatted string
BA Python dictionary
CA list
DAn integer
💡 Hint
Check the Output column at step 2 in execution_table; it shows a string with escaped quotes.
At which step does the JSON string get converted back to a Python dictionary?
AStep 1
BStep 4
CStep 3
DStep 5
💡 Hint
Look at the Action column in execution_table; json.loads is called at step 4.
If we skip json.dumps and try to print the dict directly, what changes in the output?
AOutput will be a JSON string
BOutput will be an error
COutput will be the Python dict representation
DOutput will be empty
💡 Hint
See variable_tracker for 'person' and json_str; json_str is the JSON string, person is the dict.
Concept Snapshot
Serializing: Use json.dumps(obj) to convert Python objects to JSON strings.
Deserializing: Use json.loads(json_str) to convert JSON strings back to Python objects.
JSON strings use double quotes and are text format.
Python dicts and JSON objects look similar but are different types.
Always serialize before sending/storing, deserialize when reading back.
Full Transcript
This lesson shows how to turn a Python dictionary into a JSON string using json.dumps, then back into a dictionary using json.loads. We start with a Python dict named person. Then json.dumps converts it to a JSON string with double quotes. This string can be stored or sent over a network. Later, json.loads reads the JSON string and recreates the original Python dictionary. We print both the JSON string and the restored dictionary to see the changes. Key points: json.dumps outputs a string with escaped quotes, json.loads requires a string input, and the restored object matches the original dictionary. This process is called serialization and deserialization.