Python Json.loads Valueerror, Expecting Delimiter
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 usi
Solution 1:
You can specify so called “raw strings”:
>>> printr'{"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.
Solution 2:
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!
Solution 3:
Try the ways source.replace('""', '')
or sub it, cause ""
in the source will make json.loads(source)
can not distinguish them.
Solution 4:
for my instance, i wrote:
STRING.replace("': '", '": "').replace("', '", '", "').replace("{'", '{"').replace("'}", '"}').replace("': \"", '": "').replace("', \"", '", "').replace("\", '", '", "').replace("'", '\\"')
and works like a charm.
Solution 5:
Try using triple quotes r""", no need to consider the \ thing.
json_string = r"""
{
"jsonObj": []
}
"""data = json.loads(json_string)
Post a Comment for "Python Json.loads Valueerror, Expecting Delimiter"