I am extracting a postgres table as json. The output file contains lines like:
{"data": {"test": 1, "hello": "I have \" !"}, "id": 4}
Now I need to load them in my python code using json.loads
, but I get this error:
Traceback (most recent call last):
File "test.py", line 33, in <module>
print json.loads('''{"id": 4, "data": {"test": 1, "hello": "I have \" !"}}''')
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 338, in loads
return _default_decoder.decode(s)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 365, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 381, in raw_decode
obj, end = self.scan_once(s, idx)
ValueError: Expecting , delimiter: line 1 column 50 (char 49)
I figured out the fix is to add another \
to \"
. So, if I pass
{"data": {"test": 1, "hello": "I have \\" !"}, "id": 4}
to json.loads
, I get this:
{u'data': {u'test': 1, u'hello': u'I have " !'}, u'id': 4}
Is there a way to do this without adding the extra \
? Like passing a parameter to json.loads
or something?
Try this:
If you have that string inside a variable, then just:
Hope it helps!
You can specify so called “raw strings”:
They don’t interpret the backslashes.
Usual strings change
\"
to"
, so you can have"
characters in strings that are themselves limited by double quotes:So the transformation from
\"
to"
is not done byjson.loads
, but by Python itself.Try the ways
source.replace('""', '')
or sub it, cause""
in the source will makejson.loads(source)
can not distinguish them.for my instance, i wrote:
and works like a charm.