title: Flask jsonify: how to escape characters
body: <p>I have just started working with the Flask web framework.  I am currently writing an endpoint that returns bits of JSON that may very well contain malicious javascript.</p>
  
  <p>For example:</p>
  
  <pre><code>@api.route("/tester")
  def api_jobs_tester():
      return jsonify({
          "name": "&lt;script&gt;alert(1)&lt;/script&gt;"
      })
  </code></pre>
  
  <p>In this example, this returns:</p>
  
  <pre><code>{
    "name": "&lt;script&gt;alert(1)&lt;/script&gt;"
  }
  </code></pre>
  
  <p>Ideally, however, I would like this to return:</p>
  
  <pre><code>{
    "name": "&amp;lt;script&amp;gt;alert(1)&amp;lt;/script&amp;gt;"
  }
  </code></pre>
  
  <p>Of course, this is straightfoward to do on a per-value, basis, with just:</p>
  
  <pre><code>return jsonify({
      "name": escape("&lt;script&gt;alert(1)&lt;/script&gt;")
  })
  </code></pre>
  
  <p>However, I may need to return much more complex JSON responses than this, in which I do not necessarily know before hand the structure of the JSON.  </p>
  
  <p>I could probably role my own function that traverses the JSON tree and escapes all the strings, but I would much prefer a built-in way of doing this.  </p>
  
  <p>What is the easiest way to escape the values in a JSON response using Flask?</p>