5

I've got a response object as a result of a GET request and I've converted this to JSON with jsonify(). When I pass this to a template, all I get is a JSON object like: <Response 1366 bytes [200 OK]> this.

#request.py
...
response = requests.get('http://www.example.com')
response_json = jsonify(all=response.text)

return render_template(
    'results.html',
    form=ReqForm(request.form),
    response=response_json,
    date=datetime.datetime.now()
)

and the template..

#results.html
...
<div class="results">
    {{ response }} # --> gives <Response 1366 bytes [200 OK]>
</div>
...

How can I pretty display this JSON in a template?

Leustad
  • 375
  • 1
  • 5
  • 20

1 Answers1

15

Use json.dumps

response = json.dumps(response.text, sort_keys = False, indent = 2)

or to make it prettier

response = json.dumps(response.text, sort_keys = True, indent = 4, separators = (',', ': '))

Template

#results.html
...
<div class="results">
    <pre>{{ response }}</pre>
</div>
...

The jsonify() function in flask returns flask.Response() object that already has the appropriate content-type header 'application/json' for use with json responses, whereas the json.dumps() will just return an encoded string, which would require manually adding the mime type header.

Source: https://stackoverflow.com/a/13172658/264802

gotnull
  • 26,454
  • 22
  • 137
  • 203
  • I feel like we are on the right path. I did `response = json.dumps(response.text, sort_keys=False, indent=2)` but the whole json got displayed as block. no newline or indentation. `response = json.dumps(response_json, sort_keys=False, indent=2)` returns ` is not JSON serializable` – Leustad May 20 '16 at 01:26
  • It's still displaying as a block of text. Take a look at it: http://codepen.io/anon/pen/MyMOXJ – Leustad May 20 '16 at 01:53
  • 2
    Also see the updated
     tag in the template for proper formatting.
    – gotnull May 20 '16 at 01:59
  • That `
    ` tag will help as soon as I replace those `\"` with `"` and get rid of the `"` from the start and the end
    – Leustad May 20 '16 at 02:03
  • Ok I did the above but with the `` tags it displays as 1 liner, without it it a block text again... – Leustad May 20 '16 at 02:10
  • Look at setting the proper 'application/json' MIME type. – gotnull May 20 '16 at 02:39
  • `render_template(...), {'Content-Type': 'application/json'}` makes the whole page in JSON format which is not what we want. – Leustad May 20 '16 at 02:48
  • Not sure exactly what you're after then? Consider accepting the answer, since it does fix your `` question and perhaps ask a different question? – gotnull May 20 '16 at 03:06
  • Ok, I'll do that but just to clarify; I'm trying to display this JSON on page with indents etc.. (imagine in a box) while there is other content around it. – Leustad May 20 '16 at 03:09
  • 1
    Found what I wanted to do. Since the response's content type is `application/json` I've used `.json` with the response object, like: `resp_json = json.dumps(response.json(), sort_keys=True, indent=4)` and passed `resp_json` to the template. – Leustad May 20 '16 at 05:12
  • Nice one! Glad you figured it out. – gotnull May 20 '16 at 05:16