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.
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.
Dictionary ko JSON string mein convert karna � data ko share karne ke liye.
JSON string ko dictionary mein convert karna � data ko use karne ke liye.
JSON files read aur write karna � data ko persist karne ke liye.
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
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
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
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.
JSON module ke useful functions
json.dumps(obj)� object ko JSON string mein convert karta hai.json.loads(string)� JSON string ko Python object mein convert karta hai.json.dump(obj, file)� object ko JSON file mein write karta hai.json.load(file)� JSON file se Python object read karta hai.json.dumps(obj, indent=2)� formatted output deta hai, readable hota hai.json.dumps(obj, sort_keys=True)� keys ko alphabetical order mein sort karta hai.
JSON aur Python types
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 againJSON 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
dumpsaurdumpmein confusion �dumpsstring return karta hai,dumpfile mein likhta hai.loadsaurloadmein confusion �loadsstring se padhta hai,loadfile se.- JSON keys hamesha strings hoti hain �
{"name": "Aman"}sahi hai,{name: "Aman"}galat hai. - JSON mein trailing comma allowed nahi hai �
{"a": 1,}error dega. - Single quotes JSON mein allowed nahi hain � sirf double quotes use karo.
Ab aap API se data fetch karke JSON ko Python mein parse kar sakte ho � ye skill har project mein kaam aayegi.