Developer Snippet Diary

Python Json Tutorial

Python has a built-in package called json.

import json

1.JSON TO PYTHON

import json
x =  '{ "name":"John", "age":30, "city":"New York"}'
y = json.loads(x)
print(y["age"]) #dictionary

2. PYTHON TO JSON

import json
x = {
  "name": "John",
  "age": 30,
  "city": "New York"
}
y = json.dumps(x)
print(y) #string

Python dictionary is converted to object and tuple, list is converted to array

3.READABLE JSON

json.dumps(x, indent=4)
Posted by: R GONDAL
Email: rizikmw@gmail.com