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
  changing format to Int Scott 1 26 1 hour ago
Last Post: ODIS
  Date format issue the_dude 11 504 Jun-07-2019, 06:27 AM
Last Post: snippsat
  Help with finding correct topic in Python learning yahya01 1 215 Jun-06-2019, 05:01 PM
Last Post: buran
  Changing Number Format moby 4 130 May-24-2019, 11:04 PM
Last Post: snippsat
  changing { and } in str.format() Skaperen 10 344 May-16-2019, 05:07 AM
Last Post: Skaperen
  How to continue in loop until correct input received sunnyarora 10 338 May-04-2019, 02:37 PM
Last Post: Yoriz
  error: = "NPC{}".format(num_of_NPCs + 1) Exsul 1 79 May-03-2019, 08:56 PM
Last Post: ichabod801
  Format for elif sunilramakumar 4 232 Apr-08-2019, 05:21 PM
Last Post: Yoriz
  Convert SAS Dates Format in a loop MohanReddy 2 184 Apr-02-2019, 10:31 AM
Last Post: scidam
  Help regarding date format Prince_Bhatia 2 157 Apr-01-2019, 05:46 AM
Last Post: Prince_Bhatia

Forum Jump:

Users browsing this thread: 1 Guest(s)