Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Trying to pass a nested list with JSON to JS with Flask

The problem is I’m passing my argument to the Javascript like this: link.

The variable "chatlist" is the problem. The value of that variable is

[[{"user": "lol"}, {"msg": "lol has created this chat."}], [{"user": "lol"}, {"msg": "lol"}]]

But when I try to access that variable through this code: link, it ends up looking like

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[[{"user": "lol"}, {"msg": "lol has created this chat."}], [{"user": "lol"}, {"msg": "lol"}]]

What I’m expecting is in both my Python code and my Javascript, I get

[[{"user": "lol"}, {"msg": "lol has created this chat."}], [{"user": "lol"}, {"msg": "lol"}]]

as the value of the variable labelled "chatList". This value also needs to be "parsable" (not sure if that’s the right word). I need to be able to access all elements of the list and all keys and values of each dictionary in each element.

>Solution :

Pass the list as is to render_template.

return render_template("chat.html", chatlist=chatInfo, pin=pin)

In the template you can use the jinja filter tojson.

var chatList = {{ chatlist | tojson }});
console.log(chatList);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading