Python json.loads ValueError, expecting delimiter

后端 未结 4 1203
后悔当初
后悔当初 2020-12-24 12:53

I am extracting a postgres table as json. The output file contains lines like:

{\"data\": {\"test\": 1, \"hello\": \"I have \\\" !\"}, \"id\": 4}


        
相关标签:
4条回答
  • 2020-12-24 13:20

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

    0 讨论(0)
  • 2020-12-24 13:23

    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.

    0 讨论(0)
  • 2020-12-24 13:29

    for my instance, i wrote:

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

    and works like a charm.

    0 讨论(0)
  • 2020-12-24 13:34

    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!

    0 讨论(0)
提交回复
热议问题