Python JSON

 

JSON

JSON stands for JavaScript Object Notation. It is a built-in package provided in python that is used to store and exchange data.

 

A. Converting JSON string to python:

Example:

import json

# JSON String:
colors =  '["Red", "Yellow", "Green", "Blue"]'

# JSON string to python dictionary:
lst1 = json.loads(colors)
print(lst1)

Output:

['Red', 'Yellow', 'Green', 'Blue']

 

B. Converting python to JSON string:

Example:

import json

# python dictionary
lst1 = ['Red', 'Yellow', 'Green', 'Blue']

# Convert Python dict to JSON
jsonObj = json.dumps(lst1)
print(jsonObj)

Output:

["Red", "Yellow", "Green", "Blue"]

 

C. Conversion type:

Whenever python objects are converted to JSON, object type is changed to match that of JSON.

Example:

import json

print(json.dumps(22))               #integer
print(json.dumps(6.022))            #float
print(json.dumps("Hello World"))    #string
print(json.dumps(True))             #True
print(json.dumps(False))            #False
print(json.dumps(None))             #None

Output:

22
6.022
"Hello World"
true
false
null