How to return a json object from a Python function?


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

Example

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"}

Updated on: 13-Feb-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements