I'm trying do display some JSON results in a Jinja2 HTML Template, but it returns either an empty page, or every character of the JSON on a new row, or every character with spacing.
The object that I'm sending to Jinja2 Template looks like this:
[
{
"name": "John",
"lastname": "Smith",
"age": 22,
"likes": [
{"fruits":
["bananas", "oranges"]
},
{"sweets":
["chocolate"]
}
]
}
]
To send the JSON object to the Jinja2 template I use:
render_template("report.html", items=items)
On the Jinja2 Template HTML if I use {{ items }}
it displays the full object as text.
If I'm trying to get the {{ items['name'] }}
it will not display anything on the page, the page will be completely empty.
I have also tried
{% for x in items %}
{{ x }}
{% endfor %}
and it only displays the same "text" but with spacing:
[ { " n a m e " : "J o h n " [...]
In the HTML file, I'm trying to do something like:
<div id="name">{{ items['name'] }}</div>
How can I display the objects within the JSON object I'm passing to Jinja2?
I think I got to the end of it.
I had to json_dumps
the object at first, and then to json.loads
the new object.
r = json.dumps(json_onject)
loaded_r = json.loads(r)
return render_template("report.html", items=loaded_r)
And now I can use any value from the JSON object.