JSON Handling in python in hindi in hindi
JSON Handling in Python (Hindi Guide)
JSON Handling in Python – Table of Contents (Hindi)
- What is JSON in Python – JSON क्या है Python में (in hindi)
- Python JSON Module – Python में JSON Module (in hindi)
- json.loads() in Python – JSON String को Python Object में बदलना (in hindi)
- json.dumps() in Python – Python Object को JSON String में बदलना (in hindi)
- Read JSON File in Python – Python में JSON File पढ़ना (in hindi)
- Write JSON File in Python – Python में JSON File लिखना (in hindi)
JSON Handling in Python in Hindi
आज के समय में जब भी हम Python से किसी API के साथ काम करते हैं, Data Store करते हैं, या फिर Web Application बनाते हैं, तो सबसे ज़्यादा इस्तेमाल होने वाला data format JSON होता है। इसलिए college exams और practical दोनों के लिए JSON Handling in Python एक बहुत ही important topic है। इस article में हम JSON को बिल्कुल basic level से, simple language में समझेंगे।
What is JSON in Python
JSON का full form होता है JavaScript Object Notation। यह एक lightweight data format है जिसका use data को store और transfer करने के लिए किया जाता है। Python में JSON handling का मतलब है JSON data को Python program के अंदर read, write और manipulate करना।
JSON दिखने में JavaScript जैसा लगता है, लेकिन यह language independent होता है। इसका मतलब यह है कि JSON data को Python, Java, PHP, JavaScript सभी languages समझ सकती हैं। इसी वजह से JSON को सबसे ज़्यादा APIs और Web Services में use किया जाता है।
College exams में अक्सर पूछा जाता है कि JSON क्यों popular है। इसका सबसे बड़ा reason यह है कि JSON human readable होता है और machine के लिए parse करना आसान होता है।
JSON Structure
JSON data हमेशा key-value pair में होता है। Keys हमेशा string होती हैं और values string, number, boolean, array या object हो सकती हैं। Python dictionary और JSON object काफी हद तक एक जैसे दिखते हैं।
{
"name": "Ravi",
"age": 21,
"isStudent": true
}
ऊपर दिया गया example एक simple JSON object दिखाता है। यही similarity JSON को Python developers के लिए आसान बना देती है।
Python JSON Module
Python में JSON के साथ काम करने के लिए built-in json module दिया गया है। इसका मतलब यह है कि हमें कोई external library install करने की जरूरत नहीं होती। Exam point of view से यह एक बहुत important fact है।
json module Python object को JSON में convert करने और JSON को Python object में बदलने का काम करता है। यही process serialization और deserialization कहलाती है।
Importing JSON Module
JSON handling शुरू करने से पहले हमें json module import करना होता है। यह step हर program में common रहती है।
import json
इतना लिखते ही Python हमें JSON से related सभी functions use करने की permission दे देता है। Exams में कई बार direct question आता है कि JSON module कैसे import करते हैं।
Python Object vs JSON Data
JSON और Python objects के बीच relation समझना बहुत ज़रूरी है। नीचे table के through यह relation आसान तरीके से समझ सकते हैं।
| Python Data Type | JSON Data Type |
|---|---|
| dict | object |
| list | array |
| str | string |
| int / float | number |
| True / False | true / false |
| None | null |
यह table exams में short answer या explanation type questions के लिए बहुत useful होती है।
json.loads() in Python
json.loads() function का use JSON string को Python object में convert करने के लिए किया जाता है। loads का मतलब होता है load string।
Real life में जब हमें API से data मिलता है, तो वह ज्यादातर JSON string के form में होता है। Python में उस data पर काम करने के लिए हमें उसे Python object में बदलना पड़ता है।
Working of json.loads()
json.loads() JSON string को input के रूप में लेता है और output में Python dictionary या list return करता है। यह process deserialization कहलाती है।
import json
json_data = '{"name": "Ravi", "age": 21}'
python_obj = json.loads(json_data)
print(python_obj)
print(type(python_obj))
ऊपर दिए गए code में JSON string को Python dictionary में convert किया गया है। Output में हमें dict type का object मिलता है, जिस पर हम normal Python operations apply कर सकते हैं।
Exam में अक्सर पूछा जाता है कि json.loads() और json.load() में क्या difference है। याद रखो, loads string के लिए होता है और load file के लिए।
json.dumps() in Python
json.dumps() function का use Python object को JSON string में convert करने के लिए किया जाता है। dumps का मतलब होता है dump string।
जब हमें Python data को server पर भेजना होता है, या JSON format में store करना होता है, तब json.dumps() का use किया जाता है।
Working of json.dumps()
json.dumps() Python dictionary या list को input में लेता है और output में JSON formatted string return करता है। यह process serialization कहलाती है।
import json
data = {
"course": "Python",
"level": "Beginner",
"duration": 30
}
json_string = json.dumps(data)
print(json_string)
इस example में Python dictionary को JSON string में convert किया गया है। यह JSON string आसानी से network के through transfer की जा सकती है।
College exams में यह question common है कि Python object को JSON में convert करने के लिए कौन सा function use होता है। इसका correct answer है json.dumps()।
Read JSON File in Python
अब तक हमने JSON string के साथ काम करना सीखा, लेकिन real projects और college practical में ज़्यादातर JSON data file के रूप में मिलता है। इसलिए Python में JSON file read करना बहुत important topic बन जाता है।
JSON file को read करने का मतलब है file में मौजूद JSON data को Python program के अंदर लाना ताकि उस पर processing की जा सके। यह काम Python के json.load() function से किया जाता है।
json.load() Function
json.load() function JSON file को directly Python object में convert करता है। यह function file object को input के रूप में लेता है, JSON string नहीं। Exam में यह difference बहुत बार पूछा जाता है।
json.load() का use तब होता है जब JSON data किसी external file में store हो। यह process भी deserialization कहलाती है।
Example: Reading JSON File
मान लो हमारे पास student.json नाम की एक file है, जिसमें student का data store है।
{
"name": "Amit",
"roll": 101,
"course": "Skill",
"marks": 78
}
अब इस JSON file को Python में read करने के लिए नीचे दिया गया code use किया जाता है।
import json
with open("student.json", "r") as file:
data = json.load(file)
print(data)
print(type(data))
इस code में सबसे पहले file को read mode में open किया गया है। फिर json.load() function की मदद से JSON data को Python dictionary में convert किया गया है। Output में हमें dict type का object मिलता है।
with statement का use इसलिए किया गया है ताकि file automatically close हो जाए। यह best practice मानी जाती है और exam answers में extra marks दिला सकती है।
Accessing JSON File Data
JSON file read करने के बाद data Python dictionary में होता है, इसलिए हम normal dictionary की तरह values access कर सकते हैं।
print(data["name"])
print(data["marks"])
यह concept exams में बहुत useful होता है, क्योंकि questions में अक्सर JSON file से specific value print करने को कहा जाता है।
Common Errors While Reading JSON File
JSON file read करते समय कुछ common mistakes होती हैं जिन पर examiners खास ध्यान देते हैं।
- File path गलत होना
- JSON format invalid होना
- json.load() की जगह json.loads() use कर देना
हमेशा याद रखो, file के लिए load और string के लिए loads। यही rule confusion दूर करता है।
Write JSON File in Python
JSON handling का दूसरा important हिस्सा है Python में JSON file write करना। इसका मतलब है Python object को JSON format में convert करके file में store करना।
यह concept तब use होता है जब हमें program का data permanently save करना हो या किसी दूसरे system को भेजना हो। Exams में इसे serialization कहा जाता है।
json.dump() Function
Python में JSON file write करने के लिए json.dump() function use किया जाता है। यह function Python object को JSON format में convert करके file में लिख देता है।
json.dump() function दो main arguments लेता है — पहला Python data और दूसरा file object।
Example: Writing JSON File
मान लो हमें student का data JSON file में store करना है।
import json
student_data = {
"name": "Neha",
"roll": 202,
"course": "BSc",
"result": "Pass"
}
with open("output.json", "w") as file:
json.dump(student_data, file)
इस code के run होते ही output.json नाम की file create हो जाएगी और उसमें JSON format में data save हो जाएगा। यह तरीका exams में बहुत commonly पूछा जाता है।
Using indent Parameter
By default JSON file एक single line में write होती है, जो पढ़ने में थोड़ी मुश्किल लगती है। इसे readable बनाने के लिए हम indent parameter का use करते हैं।
json.dump(student_data, file, indent=4)
indent का use करने से JSON file properly formatted हो जाती है। Exam answers में यह दिखाना एक plus point माना जाता है।
Difference Between dump() and dumps()
Students अक्सर dump() और dumps() में confuse हो जाते हैं। नीचे table से यह difference आसानी से समझ सकते हैं।
| Function | Use |
|---|---|
| json.dump() | Python object को JSON file में write करता है |
| json.dumps() | Python object को JSON string में convert करता है |
यही table college exams के short notes या difference questions में directly लिखी जा सकती है।
Practical Use of JSON Handling
JSON handling सिर्फ exam तक limited नहीं है। Real world में इसका use API data handling, configuration files और database communication में किया जाता है।
अगर तुम्हें JSON handling अच्छे से आ गई, तो Python में Web Development और Data related subjects समझना आसान हो जाता है। इसी वजह से यह topic syllabus में बार-बार आता है।
FAQs
Python में JSON Handling का मतलब होता है JSON data को Python program के अंदर read करना, write करना और process करना। JSON एक popular data format है जिसका use APIs, files और data transfer के लिए किया जाता है। Python में इसके लिए built-in json module दिया गया है।
JSON का full form JavaScript Object Notation होता है। इसका use इसलिए किया जाता है क्योंकि यह lightweight, human readable और language independent format है। Python में JSON handling आसान होती है क्योंकि JSON structure Python dictionary जैसा होता है।
json.loads() का use JSON string को Python object में convert करने के लिए किया जाता है। वहीं json.dumps() का use Python object को JSON string में बदलने के लिए होता है। Exams में loads और dumps का difference अक्सर पूछा जाता है।
Python में JSON file read करने के लिए json.load() function का use किया जाता है। सबसे पहले file को open किया जाता है, फिर json.load() से JSON data Python dictionary में convert हो जाता है। यह process deserialization कहलाती है।
Python में JSON file write करने के लिए json.dump() function use होता है। यह Python object को JSON format में convert करके file में save करता है। Readable JSON के लिए indent parameter का use करना best practice माना जाता है।
College exams में JSON Handling in Python से theory questions, short notes, difference questions और practical programs पूछे जाते हैं। जैसे json.load() vs json.loads(), json.dump() vs json.dumps() और JSON file read/write के programs।