Python and JSON
I had a requirement to have passwords contain a slash \
in an API call with JSON. However, when attempting to run json.dumps
for the credentials, Python would throw this exception:
Expecting value: line 1 column 34 (char 33)
Not surprisingly, a solution wasn’t found on Stack Overflow or any internet searches. I’m guessing the reason being \
is a reserve character in JSON similar to Python. Unfortunately, that didn’t matter as the requirements were already set and accepted, so I needed to find a fix. I attempted the following:
- Escape the
\
with two backslashes like this:\\
. - Different quotes:
'
and''
. - Encapsulating the quotes like
'"
and"'
. - Using
strict=False
forjson.loads
.- Example:
json.loads(json_creds, strict=False)
- This was the most cited workaround I found, but it never worked with the slash.
json.loads
would throw theExpecting value
exception every time.
- Example:
However, none of that worked mostly because, in all honestly, it shouldn’t. The reserver characters are there just like in Python for the language to function correctly. We wouldn’t add an @
in a method or function definition for the same reason we shouldn’t add \
in passwords for JSON. I’m digressing a bit – back to how to work around this.
I found that if the password is encoded using json.dumps
first, and then passed to the JSON URL, it worked perfectly.
password = "This.\Sample"
encoded_pw = json.dumps(password)
JSON_DATA = "{\"username\": \"" + username + "\", \"password\":" + encoded_pw + "}"
For other Python-related articles, please checkout other Python articles here.