Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unescaping Characters in a JSON response string

I made a JSON request that gives me a string that uses Unicode character codes that looks like:

s = "\u003Cp\u003E"

And I want to convert it to:

s = "<p>"

What's the best way to do this in Python?

Note, this is the same question as this one, only in Python except Ruby. I am also using the Posterous API.

like image 245
Spike Avatar asked Apr 05 '11 16:04

Spike


People also ask

What characters should be escaped in JSON?

In JSON the only characters you must escape are \, ", and control codes. Thus in order to escape your structure, you'll need a JSON specific function.

How remove all escape characters from JSON string?

I have found that the easiest and best way to remove all escape characters from your JSON string, is to pass the string into Regex. Unescape() method. This method returns a new string with no ecapes, even \n \t etc, are removed.


3 Answers

>>> "\\u003Cp\\u003E".decode('unicode-escape')
u'<p>'
like image 135
Ignacio Vazquez-Abrams Avatar answered Oct 09 '22 04:10

Ignacio Vazquez-Abrams


If the data came from JSON, the json module should already have decoded these escapes for you:

>>> import json
>>> json.loads('"\u003Cp\u003E"')
u'<p>'
like image 44
Thomas Wouters Avatar answered Oct 09 '22 04:10

Thomas Wouters


EDIT: The original question "Unescaping Characters in a String with Python" did not clarify if the string was to be written or to be read (later on, the "JSON response" words were added, to clarify the intention was to read).

So I answered the opposite question: how to write JSON serialized data dumping them to a unescaped string (rather than loading data from the string).

My use case was producing a JSON file from my own data dictionary, but the file contained scaped non-ASCII characters. So I did it like this:

with open(filename,'w') as jsonfile:
    jsonstr = json.dumps(myDictionary, ensure_ascii=False)
    print(jsonstr)          # to screen
    jsonfile.write(jsonstr) # to file

If ensure_ascii is true (the default), the output is guaranteed to have all incoming non-ASCII characters escaped. If ensure_ascii is false, these characters will be output as-is.

Taken from here: https://docs.python.org/3/library/json.html

like image 36
abu Avatar answered Oct 09 '22 03:10

abu