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?
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.
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.
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.
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.
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.
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!
Try using triple quotes r""", no need to consider the \ thing.
json_string = r"""
{
"jsonObj": []
}
"""
data = json.loads(json_string)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With