Search code examples
pythonjsonspecial-charactersbackslashrawstring

Turning python object with special character to JSON string object


I have gone through a lot of examples in stack overflow that are related to this kind of issue but none of those answers worked for my problem. I have a string inside a variable which contains the escape single backslash character "\". I am concatenating that string variable to a JSON string variable. Then doing json.loads() on that json string but I am getting an error:

ValueError: Expecting , delimiter: line 1 column 41 (char 40)

This is my code:

import json 

# The string of pass_string is taken from a rest/api/content of a website
pass_string = "I am an \"Engineer\""
data = '{"data": {"test": 1, "hello": "' + pass_string + ' in this lifetime."}, "id": 4}'

json_data = json.loads(data)
print(json_data)

Since pass_string is taken from a request.get() function from a website it is not possible to turn that into a raw string and then input into our data like:

pass_string = r"I am an \"Engineer\""

The above does work but my string is being passed inside the variable pass_string so I would have to modify the contents inside the variable somehow. Tried a lot of examples from stack overflow but none seem to work for my case.


Solution

  • json.loads needs a string representation of the escape, so you need to escape the escape:

    import json 
    
    pass_string = "I am an \"Engineer\"".replace('\"', '\\"')
    data = '{"data": {"test": 1, "hello": "' + pass_string + ' in this lifetime."}, "id": 4}'
    
    json_data = json.loads(data)
    print(json_data)
    

    Output:

    {'data': {'test': 1, 'hello': 'I am an "Engineer" in this lifetime.'}, 'id': 4}