|
प्रश्न 3. Python Dictionary को JSON String में कैसे Convert करें? |
|
प्रश्न 4. JSON String को Python Object में कैसे Convert करें? |
आज के समय में Web Applications और Software Systems के बीच Data का आदान-प्रदान बहुत महत्वपूर्ण है। इस काम के लिए JSON (JavaScript Object Notation) का व्यापक उपयोग किया जाता है।
Python में JSON Data के साथ काम करने के लिए json Module उपलब्ध है।
import json
JSON का उपयोग मुख्य रूप से—
- API में Data Transfer
- Web Applications
- Configuration Files
- Data Storage
- Client और Server के बीच Data Exchange
में किया जाता है।
JSON का पूरा नाम JavaScript Object Notation है।
यह एक Lightweight और Human-Readable Data Format है।
उदाहरण:
{
"name": "Rahul",
"age": 20,
"course": "Python"
}
यह देखने में Python Dictionary जैसा लगता है, लेकिन JSON और Python Dictionary बिल्कुल एक ही चीज नहीं हैं।
- आसानी से पढ़ा और समझा जा सकता है।
- Data Exchange के लिए उपयोगी है।
- Web APIs में व्यापक उपयोग होता है।
- विभिन्न Programming Languages के बीच Data Transfer किया जा सकता है।
- Text-based Format है।
- Key-Value Structure का उपयोग करता है।
Python में JSON के लिए:
import json
इसके प्रमुख Functions हैं—
|
Function |
कार्य |
|
json.dumps() |
Python Object → JSON String |
|
json.loads() |
JSON String → Python Object |
|
json.dump() |
Python Object → JSON File |
|
json.load() |
JSON File → Python Object |
Python और JSON में कुछ Data Types का Mapping होता है।
|
Python |
JSON |
|
dict |
Object |
|
list |
Array |
|
tuple |
Array |
|
str |
String |
|
int |
Number |
|
float |
Number |
|
True |
true |
|
False |
false |
|
None |
null |
dumps() का उपयोग Python Object को JSON String में Convert करने के लिए किया जाता है।
उदाहरण:
import json
student = {
"name": "Rahul",
"age": 20
}
data = json.dumps(student)
print(data)
Output:
{"name": "Rahul", "age": 20}
याद रखने का आसान तरीका:
dumps → Python Data को JSON String में Convert
Python Object
↓
json.dumps()
↓
JSON String
loads() JSON String को Python Object में Convert करता है।
import json
data = '{"name": "Rahul", "age": 20}'
student = json.loads(data)
print(student)
print(student["name"])
Output:
{'name': 'Rahul', 'age': 20}
Rahul
JSON String
↓
json.loads()
↓
Python Object
|
dumps() |
loads() |
|
Python → JSON |
JSON → Python |
|
String बनाता है |
Python Object बनाता है |
|
Serialization |
Deserialization |
dump() का उपयोग Python Object को सीधे JSON File में Store करने के लिए किया जाता है।
import json
student = {
"name": "Rahul",
"age": 20,
"course": "Python"
}
with open("student.json", "w", encoding="utf-8") as file:
json.dump(student, file)
इससे student.json File में Data Save हो जाएगा।
JSON File को सुंदर Format में Save करना
indent का उपयोग करके JSON को आसानी से पढ़ने योग्य बनाया जा सकता है।
import json
student = {
"name": "Rahul",
"age": 20,
"course": "Python"
}
with open("student.json", "w", encoding="utf-8") as file:
json.dump(student, file, indent=4)
File:
{
"name": "Rahul",
"age": 20,
"course": "Python"
}
load() JSON File से Data पढ़कर उसे Python Object में Convert करता है।
import json
with open("student.json", "r", encoding="utf-8") as file:
student = json.load(file)
print(student)
|
dump() |
load() |
|
Python → JSON File |
JSON File → Python |
|
File में Data Save |
File से Data Read |
|
Serialization |
Deserialization |
Python Dictionary
|
| dumps()
↓
JSON String
|
| loads()
↓
Python Dictionary
File के लिए:
Python Dictionary
|
| dump()
↓
JSON File
|
| load()
↓
Python Dictionary
import json
students = ["Rahul", "Amit", "Neha"]
data = json.dumps(students)
print(data)
Output:
["Rahul", "Amit", "Neha"]
JSON String को List में Convert करना
import json
data = '["Rahul", "Amit", "Neha"]'
students = json.loads(data)
print(students)
Nested Dictionary को JSON में Convert करना
import json
student = {
"name": "Rahul",
"marks": {
"math": 80,
"computer": 90,
"english": 75
}
}
data = json.dumps(student, indent=4)
print(data)
Python:
import json
data = {
"active": True,
"verified": False,
"address": None
}
print(json.dumps(data))
JSON में यह इस प्रकार होगा:
{
"active": true,
"verified": false,
"address": null
}
ध्यान दें:
Python True → JSON true
Python False → JSON false
Python None → JSON null
यदि JSON में Hindi या अन्य Unicode Characters हैं तो ensure_ascii=False उपयोगी है।
import json
student = {
"name": "राहुल",
"course": "Python"
}
data = json.dumps(student, ensure_ascii=False, indent=4)
print(data)
import json
student = {
"नाम": "राहुल",
"कोर्स": "Python"
}
with open("student.json", "w", encoding="utf-8") as file:
json.dump(
student,
file,
ensure_ascii=False,
indent=4
)
Practical Program 1 – Student Data JSON में Save करना
import json
student = {
"name": "Rahul",
"age": 20,
"course": "Python",
"marks": 85
}
with open("student.json", "w", encoding="utf-8") as file:
json.dump(student, file, indent=4)
print("Student data saved successfully")
Practical Program 2 – JSON File से Student Data पढ़ना
import json
with open("student.json", "r", encoding="utf-8") as file:
student = json.load(file)
print("Name:", student["name"])
print("Age:", student["age"])
print("Course:", student["course"])
print("Marks:", student["marks"])
Practical Program 3 – Multiple Students Save करना
import json
students = [
{
"name": "Rahul",
"age": 20,
"marks": 85
},
{
"name": "Amit",
"age": 21,
"marks": 78
},
{
"name": "Neha",
"age": 19,
"marks": 92
}
]
with open("students.json", "w", encoding="utf-8") as file:
json.dump(students, file, indent=4)
print("Data saved")
Practical Program 4 – Multiple Students Read करना
import json
with open("students.json", "r", encoding="utf-8") as file:
students = json.load(file)
for student in students:
print(student["name"], student["marks"])
Practical Program 5 – User से Data लेकर JSON में Save करना
import json
name = input("Enter Name: ")
age = int(input("Enter Age: "))
course = input("Enter Course: ")
student = {
"name": name,
"age": age,
"course": course
}
with open("student.json", "w", encoding="utf-8") as file:
json.dump(student, file, indent=4)
print("Data saved successfully")
Practical Program 6 – JSON String से Data निकालना
import json
data = '''
{
"name": "Rahul",
"age": 20,
"city": "Chitrakoot"
}
'''
student = json.loads(data)
print("Name:", student["name"])
print("Age:", student["age"])
print("City:", student["city"])
Practical Program 7 – JSON Data Update करना
import json
with open("student.json", "r", encoding="utf-8") as file:
student = json.load(file)
student["marks"] = 90
with open("student.json", "w", encoding="utf-8") as file:
json.dump(student, file, indent=4)
print("Data updated")
Practical Program 8 – JSON में नया Key जोड़ना
import json
with open("student.json", "r", encoding="utf-8") as file:
student = json.load(file)
student["city"] = "Chitrakoot"
with open("student.json", "w", encoding="utf-8") as file:
json.dump(student, file, indent=4)
print("New data added")
Practical Program 9 – JSON List में Student Add करना
import json
with open("students.json", "r", encoding="utf-8") as file:
students = json.load(file)
new_student = {
"name": "Pooja",
"age": 20,
"marks": 88
}
students.append(new_student)
with open("students.json", "w", encoding="utf-8") as file:
json.dump(students, file, indent=4)
print("Student added")
Practical Program 10 – JSON से Student Search करना
import json
search_name = input("Enter student name: ")
with open("students.json", "r", encoding="utf-8") as file:
students = json.load(file)
found = False
for student in students:
if student["name"].lower() == search_name.lower():
print("Student Found")
print(student)
found = True
break
if not found:
print("Student Not Found")
Serialization और Deserialization
JSON के संदर्भ में दो महत्वपूर्ण शब्द हैं—
Python Object को JSON Format में Convert करना।
json.dumps()
json.dump()
JSON को वापस Python Object में Convert करना।
json.loads()
json.load()
|
dumps() |
dump() |
|
JSON String बनाता है |
JSON File में लिखता है |
|
File की आवश्यकता नहीं |
File Object आवश्यक |
|
json.dumps(data) |
json.dump(data, file) |
|
loads() |
load() |
|
JSON String पढ़ता है |
JSON File पढ़ता है |
|
String Input |
File Object Input |
|
json.loads(data) |
json.load(file) |
Standard JSON Format में सामान्यतः Comments की सुविधा नहीं होती।
इसलिए ऐसा लिखना उचित नहीं है:
{
// student name
"name": "Rahul"
}
JSON की Practical Applications
JSON का उपयोग बहुत से क्षेत्रों में होता है—
Client और Server के बीच Data Transfer।
Application Settings Store करने में।
कई Applications JSON Format में Data Exchange करती हैं।
Apps और Servers के बीच Data Transfer।
JavaScript Applications में JSON का व्यापक उपयोग होता है।
परीक्षा की दृष्टि से महत्वपूर्ण तथ्य
- JSON का पूरा नाम JavaScript Object Notation है।
- Python में JSON के लिए json Module का उपयोग होता है।
- dumps() Python Object को JSON String में Convert करता है।
- loads() JSON String को Python Object में Convert करता है।
- dump() Python Object को JSON File में Save करता है।
- load() JSON File से Python Object प्राप्त करता है।
- dict JSON Object से संबंधित है।
- list JSON Array से संबंधित है।
- Python True → JSON true
- Python False → JSON false
- Python None → JSON null
- indent JSON को Readable Format में दिखाने के लिए उपयोगी है।
- ensure_ascii=False Unicode/Hindi Text को सही रूप में रखने में उपयोगी है।
- Serialization → Python Object से JSON।
- Deserialization → JSON से Python Object।
प्रश्न 1. JSON का पूरा नाम क्या है?
JavaScript Object Notation
प्रश्न 2. Python में JSON के लिए कौन-सा Module है?
json
प्रश्न 3. Python Dictionary को JSON String में कैसे Convert करें?
json.dumps(dictionary)
प्रश्न 4. JSON String को Python Object में कैसे Convert करें?
json.loads(json_string)
प्रश्न 5. JSON File में Data Save करने के लिए?
json.dump()
प्रश्न 6. JSON File से Data पढ़ने के लिए?
json.load()
प्रश्न 7. Serialization क्या है?
Python Object को JSON Format में Convert करना।
प्रश्न 8. Deserialization क्या है?
JSON Data को Python Object में Convert करना।
- JSON → JavaScript Object Notation
- json → Python JSON Module
- dumps() → Python → JSON String
- loads() → JSON String → Python
- dump() → Python → JSON File
- load() → JSON File → Python
- Serialization → Object → JSON
- Deserialization → JSON → Object
- indent → Readable JSON Formatting
- ensure_ascii=False → Unicode Text के लिए उपयोगी
अगला अध्याय —Python Database Connectivity
इसमें Database क्या है, SQLite, sqlite3 Module, Connection, Cursor, CREATE, INSERT, SELECT, UPDATE, DELETE, commit(), fetchone(), fetchall() और Practical Student Database Project विस्तार से होगा।