Joe
Joe

Reputation: 77

Passing an array of dictionary to Javascript

I have an array of dictionary in python, and I want to pass it to my front end to use.

This is what my pythonArrayObj looks like:

pythonArrayObj =[{"a":1,"b":2,"c":3}, {"a":4,"b":5,"c":6}, {"a":7,"b":8,"c":9}]

However, the problem is that when I do:

var test = {{ pythonArrayObj }};

It keeps giving an error due to the " quotation being converted into " if I try and do json.dumps(pythonArrayObj) before passing it to my front-end, or ' if I don't.

Does anyone know how I can fix this? I've been stuck on it for the last few days and would really appreciate the help.

Thanks!

Upvotes: 0

Views: 2041

Answers (1)

Selcuk
Selcuk

Reputation: 59219

You can use the safe filter to prevent translation of quote characters:

var test = {{ pythonArrayObj|safe }};

will result in

var test = [{'a': 1, 'c': 3, 'b': 2}, {'a': 4, 'c': 6, 'b': 5}, {'a': 7, 'c': 9, 'b': 8}]

in your HTML source.

Upvotes: 2

Related Questions