Python Forum
Return in correct json format
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Return in correct json format
#4
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 '
 'inserted"}}')
>>> 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 = requests.post('http://httpbin.org/post', 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': 'httpbin.org',
             'User-Agent': 'python-requests/2.21.0'},
 'json': {'message': 'We did it!', 'receiver': 'Bob', 'sender': 'Alice'},
 'origin': '46.246.117.13, 46.246.117.13',
 'url': 'https://httpbin.org/post'}
>>> 
>>> 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": "httpbin.org", 
    "User-Agent": "python-requests/2.21.0"
  }, 
  "json": {
    "message": "We did it!", 
    "receiver": "Bob", 
    "sender": "Alice"
  }, 
  "origin": "46.246.117.13, 46.246.117.13", 
  "url": "https://httpbin.org/post"
}
Reply


Messages In This Thread
Return in correct json format - by UtiliseIT - May-12-2019, 10:12 AM
RE: Return in correct json format - by DeaD_EyE - May-12-2019, 10:46 AM
RE: Return in correct json format - by UtiliseIT - May-13-2019, 02:10 AM
RE: Return in correct json format - by snippsat - May-13-2019, 11:24 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  format json outputs ! evilcode1 3 1,739 Oct-29-2023, 01:30 PM
Last Post: omemoe277
  Convert Json to table format python_student 2 5,460 Sep-28-2022, 12:48 PM
Last Post: python_student
  Converting cells in excel to JSON format desmondtay 4 1,732 May-23-2022, 10:31 AM
Last Post: Larz60+
  json format Bubu93200 6 1,911 Apr-23-2022, 08:59 AM
Last Post: Bubu93200
  How to return the next page from json recursively? sandson 0 1,147 Apr-01-2022, 11:01 PM
Last Post: sandson
  [split] script: remove all "carriage return" from my json variable pete 2 2,789 May-05-2020, 03:22 PM
Last Post: deanhystad
  script: remove all "carriage return" from my json variable mfran2002 4 11,182 Feb-20-2019, 05:07 AM
Last Post: mfran2002
  Return JSON records in single line using python 2.7 anandmn85 0 2,798 May-14-2018, 09:16 AM
Last Post: anandmn85
  how do I format json data in splunk? fxtyom 14 13,898 May-24-2017, 09:23 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020