JSON encoding Flask to Javascript
flask, javascript, json, python
Solution
`data` is HTML escaped, because Jinja2 by default escapes everything to be safe to embed in an HTML page.
It's much better to not encode to JSON in the view, do this in the template instead, and use the Flask `tojson` and `safe` filters.
So in the view pass in `thisdata[1]` unencoded:
return render_template(
'./index2.html', udate=thisdata[0], data=thisdata[1])
and in the view:
<script>
var myjson = {{ data|tojson|safe }};
</script>
`tojson` produces JSON data that is also HTML-safe (albeit with `"` quotes, so it is not suitable for embedding in a HTML tag attribute), and the `safe` filter can be used to switch off the HTML-encoding. There is no need to use `JSON.parse()` here, the resulting JSON produced by `tojson` is a strict JavaScript subset.
See the JSON Support section in the API documentation:
The `htmlsafe_dumps()` function of this `json` module is also available as filter called `|tojson` in Jinja2. Note that inside `script` tags no escaping must take place, so make sure to disable escaping with `|safe` if you intend to use it inside `script` tags[.]
and the Standard Filters section of the Flask Templates documentation:
`tojson()` This function converts the given object into JSON representation. This is for example very helpful if you try to generate JavaScript on the fly.
Problem
I am sitting on a Flask based webapplication. In theory I want to load a JSON file from disk and give it to javascript on the website. ``` def getData(): check_for_update() with open(LOCAL_file,"rb") as myfile: data = json.load(myfile) udate = data["today"] return (udate, data) ``` then I send it to the page with ``` return render_template('./index2.html', udate = thisdata[0], data = json.dumps(thisdata[1])) ``` Now on the page I simply try ``` <script> var myjson = JSON.parse({{data}}) </script> ``` which then results in something like this This can't not be parsed.When I copy and paste it it works fine, and python does not complain either.