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>The issue you are facing is called <a href="https://docs.python.org/2/reference/lexical_analysis.html#string-literals" rel="nofollow">character escaping</a>. The parser is indeed working as intended; this is a common topic across most programming languages.</p>

<p>In your particular example, you are trying to use double quotes <code>"</code> within a value, which itself is enclosed within double quotes, like this:</p>

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

<p>To parse the string you expected, I would do something like this:</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>which produces the following output:</p>

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

python json