Search code examples
pythonjsonpython-2.7unicodeunicode-escapes

Python - How can I convert a special character to the unicode representation?


In a dictionary, I have the following value with equals signal:

{"appVersion":"o0u5jeWA6TwlJacNFnjiTA=="}

To be explicit, I need to replace the = for the unicode representation '\u003d' (basically the reverse process of [json.loads()][1]). How can I set the unicode value to a variable without store the value with two scapes (\\u003d)?.

I've tryed of different ways, including the enconde/decode, repr(), unichr(61), etc, and even searching a lot, cound't find anything that does this, all the ways give me the following final result (or the original result):

'o0u5jeWA6TwlJacNFnjiTA\\u003d\\u003d'

Since now, thanks for your attention.

EDIT

When I debug the code, it gives me the value of the variable with 2 escapes. The program will get this value and use it to do the following actions, including the extra escape. I'm using this code to construct a json by the json.dumps() and the result returned is a unicode with 2 escapes.

Follow a print of the final result after the JSON construction. I need to find a way to store the value in the var with just one escape.

enter image description here

I don't know if make difference, but I'm doing this to a custom BURP Plugin, manipulating some selected requests.

Here is an image of my POC, getting the value of the var.

enter image description here


Solution

  • The extra backslash is not actually added, The Python interpreter uses the repr() to indicate that it's a backslash not something like \t or \n when the string containing \ gets printed: I hope this helps:

    >>> t['appVersion'] = t["appVersion"].replace('=', '\u003d')
    
    >>> t['appVersion']
    'o0u5jeWA6TwlJacNFnjiTA\\u003d\\u003d'
    
    >>> print(t['appVersion'])
    o0u5jeWA6TwlJacNFnjiTA\u003d\u003d
    
    >>> t['appVersion'] == 'o0u5jeWA6TwlJacNFnjiTA\u003d\u003d'
    True