Python dictionary escape double quotes

I need to escape double quotes when converting a dict to json in Python, but I'm struggling to figure out how.

Show

    So if I have a dict like {'foo': 'bar'}, I'd like to convert it to json and escape the double quotes - so it looks something like:

    '{\"foo\":\"bar\"}'

    json.dumps doesn't do this, and I have tried something like:

    json.dumps({'foo': 'bar'}).replace('"', '\\"') which ends up formatting like so:

    '{\\"foo\\": \\"bar\\"}'

    This seems like such a simple problem to solve but I'm really struggling with it.

    asked Jul 24, 2018 at 17:04

    8

    Your last attempt json.dumps({'foo': 'bar'}).replace('"', '\\"') is actually correct for what you think you want.

    The reason you see this:

    '{\\"foo\\": \\"bar\\"}'
    

    Is because you're printing the representation of the string. The string itself will have only a single backslash for each quote. If you use print() on that result, you will see a single backslash

    answered Jul 24, 2018 at 17:21

    nosklonosklo

    209k55 gold badges290 silver badges295 bronze badges

    What you have does work. Python is showing you the literal representation of it. If you save it to a variable and print it shows you what you're looking for.

    >>> a = json.dumps({'foo': 'bar'}).replace('"', '\\"')
    >>> print a
    {\"foo\": \"bar\"}
    >>> a
    '{\\"foo\\": \\"bar\\"}'
    

    answered Jul 24, 2018 at 17:23

    View Discussion

    Improve Article

    Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Given dictionary with string keys, remove double quotes from it.

    Input : test_dict = {‘”Geeks”‘ : 3, ‘”g”eeks’ : 9} 
    Output : {‘Geeks’: 3, ‘geeks’: 9} 
    Explanation : Double quotes removed from keys.

    Input : test_dict = {‘”Geeks”‘ : 3} 
    Output : {‘Geeks’: 3} 
    Explanation : Double quotes removed from keys. 
     

    Method #1 :  Using dictionary comprehension + replace()

    The combination of above functionalities can be used to solve this problem. In this, we perform removal of double quotes using replace() with empty string. The dictionary comprehension is used for remaking dictionary.

    Python3

    test_dict = {'"Geeks"' : 3, '"is" for' : 5, '"g"eeks' : 9}

    print("The original dictionary is : " + str(test_dict))

    res = {key.replace('"', ''):val for key, val in test_dict.items()}

    print("The dictionary after removal of double quotes : " + str(res))

    Output

    The original dictionary is : {'"Geeks"': 3, '"is" for': 5, '"g"eeks': 9}
    The dictionary after removal of double quotes : {'Geeks': 3, 'is for': 5, 'geeks': 9}

    Method #2 : Using re.sub() + dictionary comprehension

    The combination of above functions is also an alternative to solve this task. In this, we employ regex to solve the problem. 

    Python3

    import re

    test_dict = {'"Geeks"' : 3, '"is" for' : 5, '"g"eeks' : 9}

    print("The original dictionary is : " + str(test_dict))

    res = {re.sub(r'"', '', key): val for key, val in test_dict.items()}

    print("The dictionary after removal of double quotes : " + str(res))

    Output

    The original dictionary is : {'"Geeks"': 3, '"is" for': 5, '"g"eeks': 9}
    The dictionary after removal of double quotes : {'Geeks': 3, 'is for': 5, 'geeks': 9}