Error in parsing json in python
<p>Trying to parse this json in python</p>

<pre><code>'''[{"accountName":"London\"Paris\"Geneva","accountId":"1664800781","isActive":true,"timeZone":"Asia/Jerusalem","currency":"ILS"}]'''
</code></pre>

<p>gives this error</p>

<pre><code>Traceback (most recent call last):
  File "unicode_test.py", line 5, in &lt;module&gt;
    parsed_json = json.loads(json3)
  File "/usr/lib/python2.7/json/__init__.py", line 338, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python2.7/json/decoder.py", line 366, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python2.7/json/decoder.py", line 382, in raw_decode
    obj, end = self.scan_once(s, idx)
ValueError: Expecting , delimiter: line 1 column 25 (char 24)
</code></pre>

<p>whereas this json parses fine(adding extra '\')</p>

<pre><code>'''[{"accountName":"London\\"Paris\\"Geneva","accountId":"1664800781","isActive":true,"timeZone":"Asia/Jerusalem","currency":"ILS"}]'''
</code></pre>

<p>with this code:</p>

<pre><code>import json

json3 = '''[{"accountName":"London\\"Paris\\"Geneva","accountId":"1664800781","isActive":true,"timeZone":"Asia/Jerusalem","currency":"ILS"}]'''

parsed_json = json.loads(json3)
print json.dumps(parsed_json)
print parsed_json[0]['accountName']
</code></pre>

<p>But the output has me confused,
json.dumps() output</p>

<pre><code>[{"currency": "ILS", "timeZone": "Asia/Jerusalem", "accountId": "1664800781", "isActive": true, "accountName": "London\"Paris\"Geneva"}]
</code></pre>

<p>actual accountName string</p>

<pre><code>London"Paris"Geneva
</code></pre>

<p>How can I get <code>London"Paris"Geneva</code> in the json string?</p>

<p>Preceding quotes with a <code>\</code> in a string literal is called escaping it, and tells the parser that you actually mean the string contains a quote, not to end the string there. Python has two other options for that: Use a different type of quote to wrap the string, or put <code>r</code> before the string which means roughly "assume that all quotes and backslashes are already escaped."</p>

<p>What's happening here is that the <code>json</code> module is re-escaping things in the output for consistency - if you load some <code>json</code>, dump it, and then load what you dump, it shouldn't change. <code>json</code> will then read that string, so you need the double-backslash so that the quotes get escaped again then. If <code>json</code> loads a string-literal containing a double-quote, the string-literal will need to have the quote preceded by <code>\\\</code>, be wrapped with single-quotes and have the quote preceded by <code>\\</code>, or else be a raw string.</p>

<p>Simplifying your example, three things would work:</p>

<pre><code>test = "{\"accountName\": \"London\\\"Paris\\\"Geneva\"}"
</code></pre>

<p>(escape all internal quotes, and also escape the backslashes around "Paris" for later re-escaping)</p>

<p>and</p>

<pre><code>test = '{"accountName": "London\\"Paris\\"Geneva"}'
</code></pre>

<p>(Using single-quotes means you don't need to escape the double-quotes, but you still need to escape the backslashes because you <em>can</em> escape things even when you don't <em>need</em> to, and you don't actually want to in this case.)</p>

<p>and</p>

<pre><code>test = r'{"accountName": "London\"Paris\"Geneva"}'
</code></pre>

<p>(Using a raw string means you're telling it nothing is escaped, so you can use backslashes safely, but you still need to use single quotes to wrap it so that the double-quotes don't end it)</p>

<p>All three of which actually represent a string containing <code>"</code> around the key and value, with <code>\</code> before the <code>"</code> in the middle of the value: <code>'{"accountName": "London\"Paris\"Geneva"}'</code> but which will generally be printed as <code>\\</code> instead of <code>\</code> so that you know it's a literal <code>\</code>rather than an escape on the following character. That is to say:</p>

<pre><code>&gt;&gt;&gt; print test
'{"accountName": "London\\"Paris\\"Geneva"}'
&gt;&gt;&gt; print test[23]
'\\'
</code></pre>

<p>The <code>\\</code> is a representation on screen that takes two characters of space, but represents internally a single <code>\</code> character.</p>

<p>The output of <code>json.loads(test)</code>, then, is a <code>dict</code>:</p>

<pre><code>{'accountName': 'London"Paris"Geneva'}
</code></pre>

<p>And <code>json.dumps(json.loads(test)) == test</code></p>

<p>If you want <code>json</code> to dump that again, it needs to re-escape those quotes to keep things readable - if it didn't put the backslashes back in, trying to re-load that would result in an error when it hit an un-escaped quote too early. You won't be able to get <code>json</code> to dump a string that includes unescaped quotes in the middle of a string, because it is required to always dump readable JSON text which can be loaded to result in the same structure it was told to dump. If you must have the actual string contain double-quotes, your options are either:</p>

<ol>
<li>Dump it with <code>json</code>, then process it afterwards to remove the backslashes. If the output string there is still <code>test</code>, you'd run <code>test.replace(r'\"', r'"')</code></li>
<li>Process your object in some way other than by <code>json.dumps()</code>, in which case you can do it more or less however you like.</li>
</ol>

<p>Either way, though, you won't be able to read the result as JSON again without adding those back in.</p>

python json