0

I am new to json and not familiar with it.

In my Django view file, I want to pass only certain json values: school, address, each student's name (Andy, Tom, Jack) and id (Andy.id, Tom.id, Jack.id) to the html from Django view. How can I achieve the above case?

my json content is:

{
  "school": "xxx", 
  "address": "xxx", 
  "number": "xxx,
  "students": {
      "Andy": {
        "id": "xxx", 
        "add": "xxx"
      }, 
      "Tom": {
        "id": "xxx", 
        "add": "xxx"
      }, 
      "Jack": {
        "id": "xxx", 
        "add": "xxx"
      }, 
    },  
}

In my view.py, I have got these codes. jresult is the json string and I used json.dumps() to generated JSON, but it will populate all the json data to the html. How can I achieve the above case? Any helps will be appreciated.

def model_form_upload(request):
    if request.method == 'POST':
        form = FileForm(request.POST, request.FILES)
        if form.is_valid():
            file = request.FILES['file'].read()
            jresult = execution(file)
            json_string = json.dumps(jresult, indent=4)

            return render(request,
                          'virus/home.html',
                          {'json_string': json_string})

    else:
        form = FileForm()
    return render(request, 'virus/upload.html', {
        'form': form
    })

my home.html template:

<body>
    <pre>{{ json_string|safe }}</pre>
</body>
2
  • What problems are you experiencing? Commented Jul 30, 2018 at 17:45
  • @SachinKukreja So far I use json.dumps and render all the json content to the html. That is not what I want. I only want certain json value. Commented Jul 30, 2018 at 17:48

2 Answers 2

2

You can skip using json.dumps and just pass the dict object and then in template access the keys to get the values.

Example:

<!-- template -->

School: {{ json_dict.school }}
Address: {{ json_dict.address }}
<!-- and so on -->
Sign up to request clarification or add additional context in comments.

7 Comments

So my json_dict would be jresult in my code? Could you please explain a little bit detail. Thank you so much
Yes. I suppose the jresult variable is a dict object on which you are calling json.dumps to get a string literal. But, you dont need that as you want the dict object to access the values in the django template.
I cannot make it.. my code: return render(request, 'virus/home.html', jresult) and in my html: <p>{{ jresult.school }}</p>
Update to return render(request, 'virus/home.html', {'jresult': jresult })
If you keep this return render(request, 'virus/home.html', jresult), you can directly access school in html like School: {{ school }}
|
1

A JSON object is very nearly a Python dictionary. With that in mind if you want to filter for just some keys in a dictionary you can use a dictionary comprehension and write something like:

jresult = {k:v for k,v in jresult.items() if k in wanted_keys}

Before you call json.dumps on jresult. Where wanted_keys = ['school', 'address', 'number', 'students']. If you are filtering the 'students' dictionary as well you can do something similar to the above for that specific key.

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.