Alex Delin Alex Delin - 4 months ago 26
Python Question

Load JSON object including escaped json string

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.

JSON Data:

"parent": [

Trying to load from Python:

>>> import json
>>> test_string = '{"parent":["{\"key\":\"value\"}"]}'
>>> json.loads(test_string)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/", 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/", 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/", line 382, in raw_decode
obj, end = self.scan_once(s, idx)
ValueError: Expecting , delimiter: line 1 column 15 (char 14)


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

>>> '{"parent":["{\"key\":\"value\"}"]}'

Notice the \ have gone away because python is treating them as escape sequences ...

One easy fix is to use a raw string:

>>> r'{"parent":["{\"key\":\"value\"}"]}'


>>> import json
>>> test_string = r'{"parent":["{\"key\":\"value\"}"]}'
>>> json.loads(test_string)
{u'parent': [u'{"key":"value"}']}