Python JSON Tutorial | Read, Write, Parse & Process JSON Data
Modern applications constantly exchange data between servers, databases, mobile applications, websites, and APIs. One of the most popular formats for exchanging this data is JSON (JavaScript Object Notation).
Whether you’re developing a REST API using Django or FastAPI, integrating third-party services, building AI-powered applications, processing web data, or working with cloud platforms, JSON is the standard format you’ll encounter every day.
Python provides a built-in json module that makes it easy to convert Python objects into JSON (serialization) and convert JSON back into Python objects (deserialization).
In this lesson, you’ll learn how to read, write, parse, and manipulate JSON data using Python. You’ll also work with JSON files and API responses while building real-world applications.
By the end of this lesson, you’ll confidently work with JSON in web development, backend engineering, Artificial Intelligence, Machine Learning, automation, and data processing projects.
Learning Objectives
After completing this lesson, you will be able to:
- Understand JSON format.
- Convert Python objects into JSON.
- Convert JSON into Python objects.
- Read JSON files.
- Write JSON files.
- Process API responses.
- Work with nested JSON.
- Build real-world applications using JSON.
Topics Covered
- Introduction to JSON
- JSON vs Python Dictionary
- json Module
- dumps()
- loads()
- dump()
- load()
- Working with JSON Files
- Nested JSON
- API Responses
- Best Practices
Detailed Lesson Content
Introduction to JSON
Imagine you’re building an AI Student Portal.
The frontend sends student details:
{
"name":"Rahul",
"course":"AI Full Stack",
"marks":95
}
The backend receives this information and stores it in a database.
This communication happens using JSON.
JSON is the most common data exchange format used in:
- REST APIs
- Web Applications
- Mobile Apps
- AI Platforms
- Cloud Services
- Microservices
What is JSON?
JSON stands for:
JavaScript Object Notation
Although it originated from JavaScript, today it is supported by almost every programming language.
JSON is:
- Lightweight
- Human-readable
- Easy to parse
- Platform independent
Python Dictionary vs JSON
Python Dictionary
student = {
"name":"Rahul",
"marks":95
}
JSON
{
"name":"Rahul",
"marks":95
}
Main Difference:
Python Dictionary
- Uses Python objects
JSON
- Uses text format
Importing JSON Module
import json
The json module provides all JSON-related functions.
Converting Python Object to JSON
Use:
json.dumps()
Example
import json
student = {
"name":"Rahul",
"course":"Python",
"marks":95
}
data = json.dumps(student)
print(data)
Output
{"name":"Rahul","course":"Python","marks":95}
This process is called Serialization.
Converting JSON to Python Object
Use:
json.loads()
Example
import json
student = '''
{
"name":"Rahul",
"course":"Python",
"marks":95
}
'''
data = json.loads(student)
print(data["name"])
Output
Rahul
This process is called Deserialization.
Writing JSON to a File
Use:
json.dump()
Example
import json
student = {
"name":"Rahul",
"course":"Python",
"marks":95
}
with open("student.json","w") as file:
json.dump(student,file,indent=4)
print("Data Saved")
Output
Data Saved
Reading JSON File
Use:
json.load()
Example
import json
with open("student.json","r") as file:
data = json.load(file)
print(data)
Output
{'name':'Rahul','course':'Python','marks':95}
Pretty Printing JSON
Example
print(
json.dumps(
student,
indent=4
)
)
Output
{
"name": "Rahul",
"course": "Python",
"marks": 95
}
The indent parameter makes JSON easier to read.
Nested JSON
Example
student = {
"name":"Rahul",
"address":{
"city":"Jaipur",
"state":"Rajasthan"
},
"courses":[
"Python",
"Django",
"AI"
]
}
Access values
print(student["address"]["city"])
Output
Jaipur
Working with JSON Arrays
Example
students = [
{
"name":"Rahul",
"marks":90
},
{
"name":"Priya",
"marks":95
}
]
Loop
for student in students:
print(student["name"])
Output
Rahul
Priya
Working with API Responses
Most APIs return JSON.
Example
response = {
"status":"success",
"data":{
"name":"Rahul",
"course":"AI"
}
}
print(response["data"]["course"])
Output
AI
This is exactly how APIs from:
- OpenAI
- GitHub
- Stripe
- PayPal
- Weather APIs
return information.
Real-World Example 1: Student Registration
import json
student = {
"id":101,
"name":"Rahul",
"course":"Python"
}
with open(
"students.json",
"a"
) as file:
json.dump(student,file)
Real-World Example 2: Employee Records
employees = [
{
"name":"Amit",
"salary":50000
},
{
"name":"Priya",
"salary":65000
}
]
print(
json.dumps(
employees,
indent=4
)
)



