0

I pass a JSON-encoded dictionary from Python 3 to Jinja2 template and assign it to a JavaScript variable. My template is as follows

<script>
    var a = {{ json_dict }}; // is rendered as `var a = {"key": "value"};`
</script>

This works as expected, but I'd like to minify JavaScript code containing Jinja2 expressions using Closure Compiler, which currently throws predictable errors like

JSC_PARSE_ERROR: Parse error. '}' expected at line 2 character 9
var a = {{ json_dict }};

What are my options?

vaultah
  • 44,105
  • 12
  • 114
  • 143

1 Answers1

2

You wrap it in an eval or equivalent.

a = eval('({{json_dict}})')
John
  • 5,443
  • 15
  • 21