I using Jinja2 with webapp2.
Jinja2 encodes all 'context' data into unicode as their doc says. This is proving problematic when I try to insert a json string into the the template:
jsonData = json.loads(get_the_file('catsJson.txt'))
I pass jsonData to template and I'm able to loop it successfully but when I insert a json element into HTML, it looks like this:
<option value='[u'dogs', u'cats']'>
I want it to look like this (as it is in the original json string):
<option value='["dogs", "cats"]'>
Any suggestions?
In Jinja 2.9 I followed @Xion's advice to first convert the iterable elements to string using map('string')
. The map filter result I then converted to a list which is finally output as JSON using the tojson
built-in filter.
{{ jsonData|map('string')|list|tojson }}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With