Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6125425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:14:45+00:00 2026-05-23T16:14:45+00:00

I am using Flask with Jinja2 as templating language. How do you convert a

  • 0

I am using Flask with Jinja2 as templating language.

How do you convert a multidimensional Python structure to a corresponding structure in javascript using Jinja2?

Example (Python/Flask):

pyStruct = [{key1:value1, key2:value2, [{subkey1:subvalue1, subkey2:subvalue2,}]},
            {key1:value1, key2:value2, [{subkey1:subvalue1, subkey2:subvalue2,}]},]

render_template('jinjatemplate.html', pyStruct=pyStruct)

Example (Jinja2):

??

I guess what I’m asking is, can it only be done by creating convoluted loop constructs in Jinja2, or am I missing a shortcut somewhere?

If the answer is, yes, one has to use convoluted loops in Jinja2, then it’s probably a lot easier to just create the javascript code directly in python and pass this to Jinja2 for inclusion.

But that seems to defeat the purpose of using a template language like Jinja2 somewhat…

I tried (Jinja2):

{{ pyStruct|safe }}

…and this actually works as long as nothing is unicode, and doesn’t stray out of Ascii land (which it usually does in my case).

Oh, and if you wonder why pass this kind of structure? I find I often want to pass fairly complicated structures to javascript to be used by menus and other complicated selection interfaces.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T16:14:46+00:00Added an answer on May 23, 2026 at 4:14 pm

    You can use the json module, either as a Jinja filter ou directly passing the results of json.dumps() to your template.

    pyStruct = [{key1:value1, key2:value2, [{subkey1:subvalue1, subkey2:subvalue2,}]},
                {key1:value1, key2:value2, [{subkey1:subvalue1, subkey2:subvalue2,}]},]
    
    render_template('jinjatemplate.html', json_struct=json.dumps(pyStruct))
    

    In the template:

    var myStruct = ({{ json_struct|e }});
    

    Warning: I’m a bit unsure about the escaping bit (|e filter). You might want to check that the <, >, & characters are properly escaped with unicode escape sequences rather than xml entities.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Flask micro-framework 0.6 and Python 2.6 I need to get the
I am writing my website's backend using Flask and Python 2.7, and have run
I'm generating an HTML page via Python, using the Flask framework to receive messages
I'm creating a blog (and the rest of a website) using Python and Flask
I have a small application written in Python using Flask. Right now I'm running
I have a nginx + uwsgi website (using Flask for dynamic python pages). I
I'm using flask as a python framework with sqlalchemy. The models use the query_property
I'm trying Heroku with Python, I ran the hello word example with Flask successfully.
I am using the Flask micro-framework which is based on Werkzeug, which uses Python.
I am using Sqlite3 with Flask microframework, but this question concerns only the Sqlite

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.