Python में JSON data के साथ work करने के लिए built in module json provide किया गया है। जिसकी help से आप JSON data को handle कर सकते हैं।

What is JSON ?

JSON : JavaScript Object Notation की ही short form है। Mostly JSON का use data को store और exchange करने के लिए किया जाता है। JSON syntax को JavaScript Object Notation से derive किया गया है।


JSON को read और generate करने के लिए आप किसी भी programming language (like : PHP , JavaScript , Python , Java etc ..) का use कर सकते हैं। सभी का JSON format same ही होगा।

JSON Syntax

  1. JSON data key : value pair में होता है।
  2. data को comma (,) से separate किया जाता है।
  3. curly brackets { } , JSON Object को represent करता है।
  4. square brackets [], JSON Array को represent करता है।

Convert Python Data TO JSON

Python data को JSON Format में convert करने के लिए dumps() function का use किया जाता है।

Copy Fullscreen Close Fullscreen Run
# first import json module.
import json
#python dictionary .
d = {
  'name' : 'Rahul Kumar',
  'age' : 24,
  'country' : 'India'
}
# it converts into json string.
json_data = json.dumps(d)
print('JSON Format Type : ', type(json_data))
print('JSON Format : ', json_data)
Output
C:\Users\Rahulkumar\Desktop\python>python python_to_json.py
JSON Format Type :  <class 'str'>
JSON Format :  {"name": "Rahul Kumar", "age": 24, "country": "India"}

ऊपर दिए गए Example में python dictionary को JSON Format में convert किया गया है।

Convert JSON Data TO Python

JSON Data को वापस python data में convert करने के लिए loads() function का use किया जाता है।

Copy Fullscreen Close Fullscreen Run
#import json module.
import json
#json string.
json_data = '{"name": "Rahul Kumar", "age": 24, "country": "India"}'
py_data = json.loads(json_data)
print('Python Data Type : ', type(py_data))
print('Python Data : ', py_data)
Output
C:\Users\Rahulkumar\Desktop\python>python python_to_json.py
Python Data Type :  <class 'dict'>
Python Data :  {'name': 'Rahul Kumar', 'age': 24, 'country': 'India'}

हालाँकि ऊपर दिए गए दोनों example में JSON data को handle करने के लिए dictionary को use किया गया है , आप dictionary के अलावा नीचे दिए गए सभी python data type को JSON में convert कर सकते हैं।


हालाँकि python data , JSON data में किसी दूसरे तरीके से लिखा जाता जैसे Python Boolean value True : JSON में true होती है। इसी तरह बाकी data भी, नीचे दिए गए सभी python data JSON में किस type में convert होते हैं वो भी बताया गया है।

Python Data JSON Data
dictObject
listArray
tupleArray
strString
intNumber
floatNumber
Truetrue
Falsefalse
Nonenull

Example

Copy Fullscreen Close Fullscreen Run
import json
# try to convert every type of data into json.
print(json.dumps({"name": "Mohit", "age": 30})) #dictionary.
print(json.dumps(["Girish", "Shyam"])) # list.
print(json.dumps(("Aryan", "Abhishek"))) # tuple.
print(json.dumps("Welcome")) # string .
print(json.dumps(50)) # integer.
print(json.dumps(12.12)) #  float.
print(json.dumps(True)) # Boolean True.
print(json.dumps(False)) # Boolean False.
print(json.dumps(None)) # none.
Output
C:\Users\Rahulkumar\Desktop\python>python python_to_json.py
{"name": "Mohit", "age": 30}
["Girish", "Shyam"]
["Aryan", "Abhishek"]
"Welcome"
50
12.12
true
false
null

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers