Skip to content Skip to sidebar Skip to footer

Python: Json.loads Chokes On Escapes

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 ch

Solution 1:

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:

>>>classA:...  __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.

Solution 2:

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

deffix_JSON(json_message=None):
    result = Nonetry:        
        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

Solution 3:

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

You've not actually escaped the string, so it's trying to parse invalid escape codes like \d or \f. Consider using a well-tested JSON encoder, such as json2.js.

Post a Comment for "Python: Json.loads Chokes On Escapes"