sending data as JSON object from Python to Javascript with Jinja [duplicate] sending data as JSON object from Python to Javascript with Jinja [duplicate] json json

sending data as JSON object from Python to Javascript with Jinja [duplicate]


The Flask Jinja2 documentation covers this pretty well. The first example under the "Standard Filters" section shows exactly how to embed a JSON object from python into a Javascript script:

<script type=text/javascript>    doSomethingWith({{ user.username|tojson|safe }});</script>

So in this case:

var lat_lng = {{ lat_lng|tojson|safe }};

tojson calls dumps on the data, so you should pass the data directly to the template rather than calling dumps on it, otherwise you double-serialize the data and end up with a JSON string.