3

In order to better debug my api, I would like to create a debug page which shows all details of the response json obj line by line. I think it can be done by code or django template. What is the simplest way to do it?

https://developers.facebook.com/tools/explorer/?method=GET For example, facebook explorer does list details of the response json obj like this.

{
  "id": "xxx", 
  "name": "xxx", 
  "first_name": "xxx", 
  "last_name": "xxx", 
  "link": "xxx", 
  "username": "xxx", 
  "hometown": {
    "id": "xxx", 
    "name": "xxx"
  }, 
  "location": {
    "id": "xxx", 
    "name": "xxx"
  }, 
  "bio": "Drink Coffee in Whitehorse", 

  "work": [
    {
      "employer": {
        "id": "xxx", 
        "name": "xxx"
      }, 
      "location": {
        "id": "xxx", 
        "name": "xxx"
      }, 
      "position": {
        "id": "xxx", 
        "name": "xxx"
      }, 
      "start_date": "2009-01", 
      "end_date": "0000-00"
    }, 
}

2 Answers 2

9

You just need to call json.dumps() with the indent keyword argument equal to the number of spaces to indent the generated JSON by for pretty printing.

For example:

json.dumps(spam_and_eggs, indent=4)
Sign up to request clarification or add additional context in comments.

Comments

2

May I suggest using <pre></pre> in your HTML so that your json looks pretty.

import json
from django.contrib.admin.views.decorators import staff_member_required
from django.shortcuts import render
from .models import Scan

    @staff_member_required
    def info(request):
        my_info = Scan.scan()
        return render(request, 'info.html', {'info': json.dumps(my_info, indent=4)})

HTML:

<!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
        <pre>{{ info|safe }}</pre>
    </body>
</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.