Load JSON object including escaped json string
<p>I'm trying to load a JSON object from a string (via Python). This object has a single key mapped to an array. The array includes a single value which is another serialized JSON object. I have tried a few online JSON parsers / validators, but can't seem to identify what the issue with loading this object is.</p>

<p>JSON Data:</p>

<pre><code>    {
      "parent": [
        "{\"key\":\"value\"}"
      ]
    }
</code></pre>

<p>Trying to load from Python:</p>

<pre><code>&gt;&gt;&gt; import json
&gt;&gt;&gt; test_string = '{"parent":["{\"key\":\"value\"}"]}'
&gt;&gt;&gt; json.loads(test_string)
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  File "/usr/local/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 338, in loads
    return _default_decoder.decode(s)
  File "/usr/local/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 366, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/local/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 382, in raw_decode
    obj, end = self.scan_once(s, idx)
ValueError: Expecting , delimiter: line 1 column 15 (char 14)
</code></pre>

<p>If you try out your string in the REPL, you'll see pretty quickly why it doesn't work:</p>

<pre><code>&gt;&gt;&gt; '{"parent":["{\"key\":\"value\"}"]}'
'{"parent":["{"key":"value"}"]}'
</code></pre>

<p>Notice the <code>\</code> have gone away because python is treating them as escape sequences ...</p>

<p>One easy fix is to use a raw string:</p>

<pre><code>&gt;&gt;&gt; r'{"parent":["{\"key\":\"value\"}"]}'
'{"parent":["{\\"key\\":\\"value\\"}"]}'
</code></pre>

<p>e.g.</p>

<pre><code>&gt;&gt;&gt; import json
&gt;&gt;&gt; test_string = r'{"parent":["{\"key\":\"value\"}"]}'
&gt;&gt;&gt; json.loads(test_string)
{u'parent': [u'{"key":"value"}']}
</code></pre>

python json