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 return a json object from a Python function?

  • I have a python dictionary as follows

  • a = {'name':'Sarah', 'age': 24, 'isEmployed': True }

  • How do I return this dictionary from a python function as a json obect?


1 Answer
Rajendra Dharmkar

We return a json object from a python function as follows using given python dictionary.

import json
a = {'name':'Sarah', 'age': 24, 'isEmployed': True }
# a python dictionary
def retjson():
python2json = json.dumps(a)
print python2json
retjson()

OUTPUT

{"age": 24, "isEmployed": true, "name": "Sarah"}
Advertisements

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