JSON string decoding is done with the help of inbuilt method json.loads()
& json.load()
of JSON library in Python.
Here translation table show example of JSON objects to Python objects which are helpful to perform decoding in Python of JSON string.
JSON | Python |
---|---|
Object | Dict |
Array | List |
String | Unicode |
number – int | Number – int, long |
number – real | Float |
True | True |
False | False |
Null | None |
The basic JSON to Python example of decoding with the help of json.loads function:
import json # json library imported # json data string person_data = '{ "person": { "name": "Kenn", "sex": "male", "age": 28}}' # Decoding or converting JSON format in dictionary using loads() dict_obj = json.loads(person_data) print(dict_obj) # check type of dict_obj print("Type of dict_obj", type(dict_obj)) # get human object details print("Person......", dict_obj.get('person'))