Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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?

like image 875
AliBZ Avatar asked Jan 29 '15 19:01

AliBZ


People also ask

How do you convert a string to a JSON object in Python?

Use the json.loads() function. The json. loads() function accepts as input a valid string and converts it to a Python dictionary. This process is called deserialization – the act of converting a string to an object.

What is the difference between JSON load and loads?

load() is used to read the JSON document from file and The json. loads() is used to convert the JSON String document into the Python dictionary.

How do I read a JSON file in Python?

json.loads(): If you have a JSON string, you can parse it by using the json.loads() method.json.loads() does not take the file path, but the file contents as a string, using fileobject.read() with json.loads() we can return the content of the file. Example: This example shows reading from both string and JSON file.

What is JSON decode error in Python?

The Python "json. decoder. JSONDecodeError: Extra data" occurs when we try to parse multiple objects without wrapping them in an array. To solve the error, wrap the JSON objects in an array or declare a new property that points to an array value that contains the objects.


3 Answers

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.

like image 57
Gandaro Avatar answered Sep 28 '22 03:09

Gandaro


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!

like image 23
cdonts Avatar answered Sep 28 '22 03:09

cdonts


Try using triple quotes r""", no need to consider the \ thing.

json_string = r"""
{
    "jsonObj": []
}
"""
data = json.loads(json_string)
like image 43
rosa Avatar answered Sep 28 '22 02:09

rosa