Lesson 16 � Foundation

JSON:
DATA INTERCHANGE FORMAT.

JSON web APIs ki language hai � har website, har mobile app JSON use karta hai data bhejne ke liye. Python dictionaries aur JSON almost same hain. Ye lesson sikhaega ki Python mein JSON data ko kaise convert, read aur write karte hain.

? 16 min✓ BeginnerPrerequisite: datetime-math

WHY: JSON kyun zaroori hai?

Socho aap kisi website se data fetch kar rahe ho � weather app, news API, ya payment gateway. Sab kuch JSON format mein aata hai. Agar aapko web scraping, API integration, ya data storage karni hai toh JSON samajhna mandatory hai. Python ki dictionary aur JSON ki syntax almost same hai � isliye Python mein JSON handle karna bahut easy hai.

DUMPS

Dictionary ko JSON string mein convert karna � data ko share karne ke liye.

LOADS

JSON string ko dictionary mein convert karna � data ko use karne ke liye.

FILE I/O

JSON files read aur write karna � data ko persist karne ke liye.

API DATA

Web APIs se JSON data aata hai � Python mein directly dictionary mein convert hota hai.

WHEN: kab use hota hai

JSON tab use hota hai jab aapko data exchange karna ho � web APIs se data fetch karna, user preferences save karna, configuration files rakhna, ya databases mein complex data store karna. Har modern web application JSON use karta hai.

HOW: JSON operations

Dictionary to JSON string

python
import json

student = {
 "name": "Aman",
 "age": 22,
 "skills": ["Python", "SQL"]
}

# Dictionary ✓ JSON string
json_string = json.dumps(student, indent=2)
print(json_string)
# {
# "name": "Aman",
# "age": 22,
# "skills": ["Python", "SQL"]
# }

print(type(json_string)) # <class 'str'>

json.dumps() dictionary ko JSON string mein convert karta hai. indent=2 se formatted output aata hai jo readable hota hai.

JSON string to dictionary

python
import json

json_string = '{"name": "Priya", "age": 21, "city": "Delhi"}'

# JSON string ✓ dictionary
data = json.loads(json_string)
print(data["name"]) # Priya
print(data["age"]) # 21

print(type(data)) # <class 'dict'>

json.loads() JSON string ko dictionary mein convert karta hai. Ab aap normal dictionary ki tarah data access kar sakte ho.

Read/write JSON files

python
import json

student = {"name": "Aman", "age": 22, "course": "Data Science"}

# Dictionary ✓ JSON file
with open("data.json", "w") as f:
 json.dump(student, f, indent=2)

# JSON file ✓ dictionary
with open("data.json", "r") as f:
 loaded = json.load(f)
 print(loaded)
 # {'name': 'Aman', 'age': 22, 'course': 'Data Science'}

json.dump() dictionary ko file mein write karta hai. json.load() file se dictionary read karta hai. Note: dumps (string) aur dump (file) mein fark hai.

Try it: student data JSON converterDictionary banao, JSON mein convert karo, wapas parse karo
Code ko apni values se update karke run karein

JSON module ke useful functions

JSON aur Python types

python
import json

# JSON types ✓ Python types mapping
data = {
 "string": "hello", # string ✓ str
 "number_int": 42, # number ✓ int
 "number_float": 3.14, # number ✓ float
 "boolean": True, # true ✓ True
 "null_value": None, # null ✓ None
 "list": [1, 2, 3], # array ✓ list
 "nested": {"key": "val"} # object ✓ dict
}

json_str = json.dumps(data)
parsed = json.loads(json_str)

print(parsed)
# Notice: True becomes true in JSON string
# But back in Python, it's True again

JSON mein true, false, null hote hain. Python mein ye True, False, None ban jaate hain. Dono taraf automatic conversion hota hai.

Quick check

import json likho aur ek dictionary ko JSON string mein convert karo.

Pehle import json likho, phir json.dumps() use karke dictionary convert karo.

Common mistakes

JSON clear?

Ab aap API se data fetch karke JSON ko Python mein parse kar sakte ho � ye skill har project mein kaam aayegi.