title: JSON from python json.dumps to javascript JSON.parse() with escaped \"
body: <p>I have been fighting with this problem for a few weeks now and still cannot understand what's wrong here.
  I created a dictionary in python <code>dic</code>. Then I am using dumps to convert it to a valid json.</p>
  
  <pre><code>json_js = json.dumps(dic) # works fine, a valid json from the python's viewpoint
  # the reverse operation works fine also
  dic = json.loads(json_js)
  </code></pre>
  
  <pre><code>print(json_js)
  ==============
  {"p0": {"pf": {"id": "pf1", "class": ["pf", "w0", "h0"], "data-page-no": "1"}, "pc": {"class": ["pc", "pc1", "w0", "h0"]}, "img": ["&lt;img alt=\"\" clas
  </code></pre>
  
  <p>This json_js I use later on to add it to a js script that applies <code>JSON.parse()</code> on it. And this is the error that I get.</p>
  
  <pre><code>SyntaxError: JSON.parse: expected ',' or ']' after array element at line 1 column 143 of the JSON data
  </code></pre>
  
  <p>That 143 character is the first <code>\"</code>. But why can't js figure it as a valid JSON I cannot comprehend.
  Would you have any suggestions what might have gone wrong here?</p>
  
  <p>EDIT.
  No idea why people close my question. The desired behaviour is that <code>JSON.parse</code> doesn't throw any errors. The way how I added it to a script is irrelevant for the question. Please, have a look at the part of the source code inside html .</p>
  
  <pre><code>const str = `{"p0": {"pf": {"id": "pf1f", "class": ["pf", "w2", "h2"], "data-page-no": "1f"}, "pc": {"class": ["pc", "pc1f", "w2", "h2"]}, "img": ["&lt;img alt=\"\" class=\"bi x0 y0 w1 h1\"` // this string is several megabytes so I only put the first 150 or so characters here.
  var dic = JSON.parse(str);
  </code></pre>
  
  <p>EDIT 2.
  The full transformation.</p>
  
  <pre><code># in python using BeautifulSoup for scripts and new_html
  new_html = bs()
  dom = bs()
  scripts = [dom.new_tag('script')]
  scripts[0].string = html_script(json_js) # html_script is a "string... %s" %json_js
  new_html.body.append(scripts[0])
  with open('stuff.html','w',encoding='utf-8') as f: 
          f.write(str(new_html))
  </code></pre>