Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jinja2 - How to loop a json list?

How can I loop a json list with jinja2?

I have this json list,

[
    {
        "first_name": "John",
        "last_name": "Smith",
        "user_id": 4,
        "address": null
    },
    {
        "first_name": "Jane",
        "last_name": "Heart",
        "user_id": 5,
        "address": null
    },
    {
        "first_name": "Dom",
        "last_name": "Robinsons",
        "user_id": 6,
        "address": null
    },
    {
        "first_name": "Pete",
        "last_name": "Hand",
        "user_id": 7,
        "address": null
    }
]

page.html,

<table>
   {% for user in users %}
   <tr><td>{{ user.first_name }}</td></tr>
   {% endfor %}
</table>

Result,

<table>

   <tr><td></td></tr>

   <tr><td></td></tr>

   <tr><td></td></tr>

   <tr><td></td></tr>
   ...

Any ideas?

like image 318
Run Avatar asked Aug 19 '15 02:08

Run


People also ask

How do you loop a JSON array?

To iterate JSON array, use the JSON. parse().

Can you loop through JSON?

To loop through a JSON array with JavaScript, we can use a for of loop. to loop through the json array with a for of loop. We assign the entry being looped through to obj . Then we get the value of the id property of the object in the loop and log it.

How do you write a for loop in Jinja2?

Jinja2 being a templating language has no need for wide choice of loop types so we only get for loop. For loops start with {% for my_item in my_collection %} and end with {% endfor %} . This is very similar to how you'd loop over an iterable in Python.


2 Answers

your json list contains dictionaries; you need to access the dictionary elements differently than you would class members; try:

<tr><td>{{ user['first_name'] }}</td></tr>

this works for me (python 3.4 and python 2.7)

import json
from jinja2 import Template

json_str = '''[{"first_name": "John", "last_name": "Smith", "user_id": 4, 
    "address": null}, {"first_name": "Jane", "last_name": "Heart",
    "user_id": 5, "address": null}, {"first_name": "Dom",
    "last_name": "Robinsons", "user_id": 6, "address": null},
    {"first_name": "Pete", "last_name": "Hand", "user_id": 7,
    "address": null}]'''

users = json.loads(json_str)

tmpl = Template('''
<table>
   {% for user in users %}
   <tr><td>{{ user['first_name'] }}</td></tr>
   {% endfor %}
</table>
''')

print(tmpl.render(users = users))

output:

<table>

   <tr><td>John</td></tr>

   <tr><td>Jane</td></tr>

   <tr><td>Dom</td></tr>

   <tr><td>Pete</td></tr>

</table>
like image 85
hiro protagonist Avatar answered Sep 17 '22 13:09

hiro protagonist


simple json iteration in jinja2

<table>
   <tr>
       {% for key in users[0] %}
       <th>{{ key }}</th>
       {% endfor %}
   </tr>

   {% for user in users %}
   <tr>
       {% for key in user %}
       <td>{{ user[key] }}</td>
       {% endfor %}
   </tr>
   {% endfor %}
</table>
like image 38
Majid Zandi Avatar answered Sep 20 '22 13:09

Majid Zandi