Decode complex JSON in Python
Asked Answered
T

3

5

I have a JSON object created in PHP, that JSON object contains another escaped JSON string in one of it's cells:

php > $insidejson = array('foo' => 'bar','foo1' => 'bar1');
php > $arr = array('a' => array('a1'=>json_encode($insidejson)));
php > echo json_encode($arr);
{"a":{"a1":"{\"foo\":\"bar\",\"foo1\":\"bar1\"}"}}

Then, with Python, I try deocding it using simplejson:

>>> import simplejson as json
>>> json.loads('{"a":{"a1":"{\"foo\":\"bar\",\"foo1\":\"bar1\"}"}}')

This fails with the following error:

Traceback (most recent call last):
  File "", line 1, in ?
  File "build/bdist.linux-i686/egg/simplejson/__init__.py", line 307, in loads
  File "build/bdist.linux-i686/egg/simplejson/decoder.py", line 335, in decode
  File "build/bdist.linux-i686/egg/simplejson/decoder.py", line 351, in raw_decode
ValueError: Expecting , delimiter: line 1 column 14 (char 14)

How can I get this JSON object decoded in Python? Both PHP and JS decode it successfully and I can't change it's structure since that would require major changes in many different components in different languages.

Thanks!

Terence answered 4/8, 2009 at 23:5 Comment(0)
Y
9

Try prefixing your string with 'r' to make it a raw string:

# Python 2.6.2
>>> import json
>>> s = r'{"a":{"a1":"{\"foo\":\"bar\",\"foo1\":\"bar1\"}"}}'
>>> json.loads(s)
{u'a': {u'a1': u'{"foo":"bar","foo1":"bar1"}'}}

What Alex says below is true: you can just double the slashes. (His answer was not posted when I started mine.) I think that using raw strings is simpler, if only because it's a language feature that means the same thing and it's harder to get wrong.

Yahairayahata answered 4/8, 2009 at 23:38 Comment(1)
Yes I agree hughdbbrown, I had forgotten about Python's raw string feature. +1Neanderthal
B
1

Try

Maybe simplejson is too much "simple".

Bakke answered 4/8, 2009 at 23:19 Comment(1)
"simplejson is the externally maintained development version of the json library included with Python 2.6 and Python 3.0"Neanderthal
N
1

If you want to insert backslashes into a string they need escaping themselves.

import simplejson as json
json.loads('{"a":{"a1":"{\\"foo\\":\\"bar\\",\\"foo1\\":\\"bar1\\"}"}}')

I've tested it and Python handles that input just fine - except I used the json module included in the standard library (import json, Python 3.1).

Neanderthal answered 4/8, 2009 at 23:35 Comment(2)
thanks! this one also works but I prefer using hughdbrown's solution since it requires no changes in other componentsTerence
The solutions are equivalent, but using a raw string does look neater :)Neanderthal

© 2022 - 2024 — McMap. All rights reserved.