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 9151949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:55:31+00:00 2026-06-17T11:55:31+00:00

How do you safely render JSON data in a django webapp? On the server

  • 0

How do you safely render JSON data in a django webapp?

On the server in django I generate JSON data and then render that JSON data in a django template. The JSON occasionally contains snippets of html. Most of the time, that’s fine, however if the </script> tag is inside the JSON data when it is rendered, it destroys the surrounding javascript.

For example…

On the server, in python I’ll have this:

template_data = {
    'my_json' : '[{"my_snippet": "<b>Happy HTML</b>"}]'
}
# pass the template data to the django template
return render_to_response('my_template.html', template_data, context_instance = c)

And then in the template:

<script type="text/javascript">
var the_json = {{my_json|safe}};
</script>
... some html ...

The resulting html works fine and looks like this:

<script type="text/javascript">
var the_json = [{"my_snippet": "<b>Happy HTML</b>"}];
</script>
... some html ...

However, you run into problems when, on the server, the JSON looks like this:

template_data = {
    'my_json' : '[{"my_snippet": "Bad HTML</script>"}]'
}
return render_to_response('my_template.html', template_data, context_instance = c)

Now, when it’s rendered, you’ll get:

<script type="text/javascript">
var the_json = [{"my_snippet": "Bad HTML</script>"}];
</script>
... some html ...

The closing script tag within the JSON code is treated as closing the entire script block. All of your javascript will then break.

One possible solution is to check for </script> when passing the template data to the template, but I feel like there is a better way.

  • 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-06-17T11:55:33+00:00Added an answer on June 17, 2026 at 11:55 am

    Safely insert the JSON as a string, and then call JSON.parse on it

    Use escapejs instead of safe. It is designed for outputting to JavaScript.

    var the_json = '{{my_json|escapejs}}';
    

    To get a JavaScript object you then need to call JSON.parse on that string. This is always preferable than dumping a JSON-encoding into your script and evaluating it directly, for security reasons.

    A useful filter to get python objects directly to the client that I use is this:

    @register.filter
    def to_js(value):
        """
        To use a python variable in JS, we call json.dumps to serialize as JSON server-side and reconstruct using
        JSON.parse. The serialized string must be escaped appropriately before dumping into the client-side code.
        """
        # separators is passed to remove whitespace in output
        return mark_safe('JSON.parse("%s")' % escapejs(json.dumps(value, separators=(',', ':'))))
    

    And use it like:

    var Settings = {{ js_settings|to_js }};
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to know how to safely update data when programming with Entity
From MSDN doc : A delegate is a type that safely encapsulates a method,
I have a comment form where I am trying to render an HTML link
Could I safely include the httpclient-4.0.1 jar that replaced the commons-httpclient jar that is
How to safely delete file content? I mean data should be unrecoverably wiped, so
This is my template; <script id=DepartmentTemplate type=text/x-jquery-tmpl> {{ for BusinessUnitName }} <li>{{:#data}}</li> {{/for }}
I tried to find out how to safely assert that preferences aren't stored anywhere.
I am using jinja2 to safely render templates for email messages from a web
I want to make a method that safely stops a thread running in a
Which character do I need to replace to safely 'print' raw HTML code from

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.