Passwords with non-standard Characters in JSON using Python

Path less traveled

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:

  1. Escape the \ with two backslashes like this: \\.
  2. Different quotes: ' and ''.
  3. Encapsulating the quotes like '" and "'.
  4. Using strict=False for json.loads.
    1. Example: json.loads(json_creds, strict=False)
    2. This was the most cited workaround I found, but it never worked with the slash. json.loads would throw the Expecting value exception every time.

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.