Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Return in correct json format

Trying to nail my return statement and need to make sure my body message is in correct json format.

My attempt so far

return {
        'statusCode': 200,
        'headers': {'Access-Control-Allow-Origin': '*',
        'Content-Type': 'application/json'},
        'body': {'message': 'Successfully inserted'}
It's only the body part that I haven't got right.

Any assistance is gratefully appreciated


This is a Python dict. The Python dict can converted with json.dumps(your_dict) to a json-string.

If you use flask:

from flask import jsonify

# your code

def root():
    message = {
        'statusCode': 200,
        'headers': {'Access-Control-Allow-Origin': '*',
        'Content-Type': 'application/json'},
        'body': {'message': 'Successfully inserted'}
    return jsonify(message)
The jsonify function sets also the right content type in the headers.
Other frameworks have also helpers for this task.
UtiliseIT likes this post
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die:
All humans together. We don't need politicians!
Thanks @DeaD_EyE for your response.

Is there a way to do this without having to import anything apart from
import json
Can do dumps and loads.
(May-13-2019, 02:10 AM)UtiliseIT Wrote: Is there a way to do this without having to import anything apart from
Yes,but it depend what of you a trying to do.
json dumps and loads.
>>> import json
>>> message
{'body': {'message': 'Successfully inserted'},
 'headers': {'Access-Control-Allow-Origin': '*',
             'Content-Type': 'application/json'},
 'statusCode': 200}
>>> d = json.dumps(message)
>>> d
('{"statusCode": 200, "headers": {"Access-Control-Allow-Origin": "*", '
 '"Content-Type": "application/json"}, "body": {"message": "Successfully '
>>> j = json.loads(d)
>>> j
{'body': {'message': 'Successfully inserted'},
 'headers': {'Access-Control-Allow-Origin': '*',
             'Content-Type': 'application/json'},
 'statusCode': 200}

Most of the time is not needed to make a HTTP message body yourself.
As show with Flask jsonify, it will dumps with mimetype='application/json'.
Same with Requests if eg try to send POST data.
Just send data and the underlying stuff is been take care of,using httpbin can test stuff out.
>>> import requests
>>> from pprint import pprint
>>> data = {'sender': 'Alice', 'receiver':'Bob', 'message':'We did it!'}
>>> r ='', json=data)
>>> pprint(r.json())
{'args': {},
 'data': '{"sender": "Alice", "receiver": "Bob", "message": "We did it!"}',
 'files': {},
 'form': {},
 'headers': {'Accept': '*/*',
             'Accept-Encoding': 'gzip, deflate',
             'Content-Length': '63',
             'Content-Type': 'application/json',
             'Host': '',
             'User-Agent': 'python-requests/2.21.0'},
 'json': {'message': 'We did it!', 'receiver': 'Bob', 'sender': 'Alice'},
 'origin': ',',
 'url': ''}
>>> print(r.text)
  "args": {}, 
  "data": "{\"sender\": \"Alice\", \"receiver\": \"Bob\", \"message\": \"We did it!\"}", 
  "files": {}, 
  "form": {}, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Content-Length": "63", 
    "Content-Type": "application/json", 
    "Host": "", 
    "User-Agent": "python-requests/2.21.0"
  "json": {
    "message": "We did it!", 
    "receiver": "Bob", 
    "sender": "Alice"
  "origin": ",", 
  "url": ""

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Incorrect time format KoSik 5 125 Aug-15-2019, 05:10 PM
Last Post: KoSik
  Not quite getting the correct Output from a function twinpiques 3 156 Aug-04-2019, 11:53 PM
Last Post: twinpiques
  read text file and write into html with correct link jacklee26 4 153 Aug-02-2019, 05:48 AM
Last Post: jacklee26
  Definition of .format? (Newbie question) Jan_97 4 234 Jul-13-2019, 12:49 PM
Last Post: Jan_97
  Why does this return False? gunitinug 1 97 Jul-13-2019, 07:11 AM
Last Post: perfringo
  Need help with 'return' in python Leo12143 2 277 Jul-10-2019, 02:35 AM
Last Post: SheeppOSU
  changing format to Int Scott 3 274 Jun-19-2019, 06:33 AM
Last Post: ODIS
  Date format issue the_dude 11 738 Jun-07-2019, 06:27 AM
Last Post: snippsat
  Help with finding correct topic in Python learning yahya01 1 282 Jun-06-2019, 05:01 PM
Last Post: buran
  Changing Number Format moby 4 233 May-24-2019, 11:04 PM
Last Post: snippsat

Forum Jump:

Users browsing this thread: 1 Guest(s)