Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

How to pass a json object as a parameter to a python function?

I have a json object as follows

json_string = '{"name":"Rupert", "age": 25, "desig":"developer"}'

How to pass this json object to a python function?


1 Answer
Rajendra Dharmkar

We have this code below which will the given json object as a parameter to a python function.

import json
json_string = '{"name":"Rupert", "age": 25, "desig":"developer"}'
print type (json_string)
def func(strng):
    a =json.loads(strng)
    print type(a)
    for k,v in a.iteritems():
           print k,v
    print dict(a)      
func(json_string)

 OUTPUT

<type 'str'>
<type 'dict'>
desig developer
age 25
name Rupert
{u'desig': u'developer', u'age': 25, u'name': u'Rupert'}

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.