Master Python JSON: Read, Write, Parse, and Serialize with Real Code
This tutorial walks you through Python's built‑in JSON handling, covering reading JSON files, converting between JSON strings and Python objects, using pandas for JSON parsing, and applying serialization, deserialization, and pretty‑printing techniques with complete code examples.
Python JSON Overview
JSON (JavaScript Object Notation) is a lightweight, text‑based data interchange format inspired by JavaScript. It is widely used to store and transmit semi‑structured data between browsers and servers.
Reading JSON Files in Python
Python’s built‑in json module can read JSON files without extra installation. Example:
import json
with open('data.json') as f:
data = json.load(f)
print(data)Parsing JSON Strings
Use json.loads() to convert a JSON string into a Python dictionary or list, and json.dumps() to convert Python objects back to JSON strings.
import json
people_string = '''
{
"people": [
{"emp_name": "John smith", "emp_no.": "924367-567-23", "emp_email": ["[email protected]"], "has_license": false},
{"emp_name": "harshit kant", "emp_number": "560-555-5153", "emp_email": null, "has_license": true}
]
}
'''
data = json.loads(people_string)
print(data)
print(type(data))Converting Python Objects to JSON
Use json.dumps() to serialize Python data structures into JSON strings.
import json
new_string = json.dumps(data, sort_keys=True, indent=3)
print(new_string)Parsing JSON with Pandas
Pandas can directly read JSON files into DataFrames.
import pandas as pd
import json
with open('nobel_prize.json') as f:
data = json.load(f)
df = pd.DataFrame(data)
print(df)JSON Serialization (Encoding)
Serialization converts Python objects (e.g., dict) into JSON using json.dump() for files or json.dumps() for strings.
import json
with open('new_nobel_prize.json', 'w') as f:
json.dump(data, f, indent=2)Key functions: dump() – write JSON to a file dumps() – return JSON string load() – read JSON file into Python loads() – parse JSON string into Python
Pretty Printing JSON
Use json.dumps() with sort_keys=True and indent to produce human‑readable output.
import json
pretty = json.dumps(data, sort_keys=True, indent=3)
print(pretty)JSON Deserialization (Decoding)
Deserialization reverses serialization, converting JSON back to Python objects using json.load() or json.loads().
Full Example: Serialize and Deserialize a Nobel Prize Dataset
import json
# Serialize
with open('nobel_prize.json') as f:
data = json.load(f)
with open('new_nobel_prize.json', 'w') as f:
json.dump(data, f, indent=2)
# Deserialize and iterate
with open('nobel_prize.json') as f:
data = json.load(f)
for prize in data['prizes']:
print(prize['year'], prize['category'])Output shows each prize’s year and category.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Huawei Cloud Developer Alliance
The Huawei Cloud Developer Alliance creates a tech sharing platform for developers and partners, gathering Huawei Cloud product knowledge, event updates, expert talks, and more. Together we continuously innovate to build the cloud foundation of an intelligent world.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
