Python json.loads ValueError, expecting delimiter

2020-05-18 11:14发布

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?

4条回答
Rolldiameter
2楼-- · 2020-05-18 11:53

Try this:

json.loads(r'{"data": {"test": 1, "hello": "I have \" !"}, "id": 4}')

If you have that string inside a variable, then just:

json.loads(data.replace("\\", r"\\"))

Hope it helps!

查看更多
Rolldiameter
3楼-- · 2020-05-18 11:56

You can specify so called “raw strings”:

>>> print r'{"data": {"test": 1, "hello": "I have \" !"}, "id": 4}'
{"data": {"test": 1, "hello": "I have \" !"}, "id": 4}

They don’t interpret the backslashes.

Usual strings change \" to ", so you can have " characters in strings that are themselves limited by double quotes:

>>> "foo\"bar"
'foo"bar'

So the transformation from \" to " is not done by json.loads, but by Python itself.

查看更多
够拽才男人
4楼-- · 2020-05-18 11:59

Try the ways source.replace('""', '') or sub it, cause "" in the source will make json.loads(source) can not distinguish them.

查看更多
仙女界的扛把子
5楼-- · 2020-05-18 12:11

for my instance, i wrote:

STRING.replace("': '", '": "').replace("', '", '", "').replace("{'", '{"').replace("'}", '"}').replace("': \"", '": "').replace("', \"", '", "').replace("\", '", '", "').replace("'", '\\"')

and works like a charm.

查看更多
登录 后发表回答