I’m using Python 3.8, and requests
to make API calls
I’m receiving from an API JSON datas with double backslash.
"address_1": "\",
They seems to be lost when using the .json()
method of requests.Response
.
So when I save those datas into an mariaDb database, and try to use JSON function, maria complains of invalid JSON.
Why?
First I try to test json.loads()
Guess a user type hello into an input on a webpage
I want to transform this valid JSON object into a dict
{"input": "hel\lo"}
Why do I get an exception with this code:
string = '{"input": "hel\lo"}'
dict = json.loads(string)
print(dict)
Invalid escape: line 1 column 15 (char 14)
File "/home/michel/sources/test.py", line 5, in <module>
dict = json.loads(string)
json.decoder.JSONDecodeError: Invalid escape: line 1 column 15 (char 14)
but not with this one:
string = '{"input": "hel\lo"}'
dict = json.loads(string)
print(dict)
string = json.dumps(dict)
String value with VS Code is '{"input": "hel\\lo"}'
1