Computer >> Computer tutorials >  >> Programming >> Python

How to convert JSON data into a Python object?


The following code converts a json object(string) into a python object(dictionary). We import the json module and use the json.loads() method to do this.

Example

import json
json_string = '{"name":"Sonali", "age": 21, "designation":" Software developer"}'
print type (json_string)
def func(strng):
    a =json.loads(strng)
    print type(a)
    print a
func(json_string)

output

<type 'str'>
<type 'dict'>
{u'age': 21, u'name': u'Sonali', u'designation': u'Software developer'}