Search code examples
pythonjsonescaping

How to json decode {"prop": "b=D:\\C"} with python (single backslash problem)


According to https://jsonlint.com/ this is valid json. Why can't python decode it? How to decode this in python?

{
    "prop": "b=D:\\C"
}

Python code:

s = '{"prop": "b=D:\\C"}'
l = json.loads(s)
print(l)

I thought it was easy to get b=D:\C as a result.

But:

json.decoder.JSONDecodeError: Invalid \escape: line 1 column 15 (char 14)

Solution

  • Try using raw string:

    s = r'{"prop": "b=D:\\C"}' # note the "r" before string
    l = json.loads(s)
    print(l)