Python JSON Parser Example | How To Parse JSON In Python
Chapter:
Python
Last Updated:
25-06-2021 08:48:23 UTC
Program:
/* ............... START ............... */
import json
# some JSON:
person = '{ "name":"Mathew", "age":28, "city":"Delhi"}'
# parse x:
y = json.loads(person)
# the result is a Python dictionary:
print(y["age"])
/* Output
28
*/
import json
# some JSON:
person = { "name":"Mathew",
"age":28,
"city":"Delhi"}
# convert into JSON:
y = json.dumps(person)
# the result is a JSON string:
print(y)
/* Output
{"name": "Mathew", "age": 28, "city": "Delhi"}
*/
/* ............... END ............... */
Notes:
-
In order to work with json data , python has a built-in package called json. Using "import json" we can import the same in our program.
- By using json.loads() we can easily parse the JSON string. Please refer the above program for more details.
- By using the funtion json.dumps() we can convert a python object to JSON string.