Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: json.loads chokes on escapes

Tags:

I have an application that is sending a JSON object (formatted with Prototype) to an ASP server. On the server, the Python 2.6 "json" module tries to loads() the JSON, but it's choking on some combination of backslashes. Observe:

>>> s '{"FileExists": true, "Version": "4.3.2.1", "Path": "\\\\host\\dir\\file.exe"}'  >>> tmp = json.loads(s) Traceback (most recent call last):   File "<interactive input>", line 1, in <module>   {... blah blah blah...}   File "C:\Python26\lib\json\decoder.py", line 155, in JSONString     return scanstring(match.string, match.end(), encoding, strict)   ValueError: Invalid \escape: line 1 column 58 (char 58)  >>> s[55:60] u'ost\\d' 

So column 58 is the escaped-backslash. I thought this WAS properly escaped! UNC is \\host\dir\file.exe, so I just doubled up on slashes. But apparently this is no good. Can someone assist? As a last resort I'm considering converting the \ to / and then back again, but this seems like a real hack to me.

Thanks in advance!

like image 426
Chris Avatar asked Oct 01 '09 17:10

Chris


People also ask

How do you unescape a JSON string in Python?

To un-escape a backslash-escaped string with Python, we can use the string encode and decode method. to call encode with 'raw_unicode_escape' to encode the string as an escaped string. Then we call decode with 'unicode_escape' to unescape the string.

What JSON loads does in Python?

loads() method can be used to parse a valid JSON string and convert it into a Python Dictionary. It is mainly used for deserializing native string, byte, or byte array which consists of JSON data into Python Dictionary.

What is JSON dumps in Python?

The json. dumps() method allows us to convert a python object into an equivalent JSON object. Or in other words to send the data from python to json. The json. dump() method allows us to convert a python object into an equivalent JSON object and store the result into a JSON file at the working directory.


2 Answers

The correct json is:

r'{"FileExists": true, "Version": "4.3.2.1", "Path": "\\\\host\\dir\\file.exe"}' 

Note the letter r if you omit it you need to escape \ for Python too.

>>> import json >>> d = json.loads(s) >>> d.keys() [u'FileExists', u'Path', u'Version'] >>> d.values() [True, u'\\\\host\\dir\\file.exe', u'4.3.2.1'] 

Note the difference:

>>> repr(d[u'Path']) "u'\\\\\\\\host\\\\dir\\\\file.exe'" >>> str(d[u'Path']) '\\\\host\\dir\\file.exe' >>> print d[u'Path'] \\host\dir\file.exe 

Python REPL prints by default the repr(obj) for an object obj:

>>> class A: ...   __str__ = lambda self: "str" ...   __repr__  = lambda self: "repr" ...  >>> A() repr >>> print A() str 

Therefore your original s string is not properly escaped for JSON. It contains unescaped '\d' and '\f'. print s must show '\\d' otherwise it is not correct JSON.

NOTE: JSON string is a collection of zero or more Unicode characters, wrapped in double quotes, using backslash escapes (json.org). I've skipped encoding issues (namely, transformation from byte strings to unicode and vice versa) in the above examples.

like image 106
jfs Avatar answered Sep 21 '22 15:09

jfs


Since the exception gives you the index of the offending escape character, this little hack I developed might be nice :)

def fix_JSON(json_message=None):     result = None     try:                 result = json.loads(json_message)     except Exception as e:               # Find the offending character index:         idx_to_replace = int(str(e).split(' ')[-1].replace(')', ''))                 # Remove the offending character:         json_message = list(json_message)         json_message[idx_to_replace] = ' '         new_message = ''.join(json_message)              return fix_JSON(json_message=new_message)     return result 
like image 23
Blairg23 Avatar answered Sep 20 '22 15:09

Blairg23