python - Unable to load json containing escape sequences -
i'm being passed json , having trouble parsing it.
the object simple single key/value pair. key works fine value \d causes issues.
this coming html form, via javascript. of below literals.
- html:
\d - javascript:
{'key': '\d'} - json:
{"key": "\\d"}
json.loads() doesn't seem json in format. quick sanity check i'm not doing silly works fine:
>>> import json >>> json.loads('{"key":"value"}') {'key': 'value'} since i'm declaring string in python, should escape down literal of va\\lue - which, when parsed json should va\lue.
>>> json.loads('{"key":"va\\\\lue"}') {'key': 'va\\lue'} in case python wasn't escaping string on way in, thought i'd check without doubling...
>>> json.loads('{"key":"va\\lue"}') traceback (most recent call last): file "<stdin>", line 1, in <module> file "c:\python33\lib\json\__init__.py", line 319, in loads return _default_decoder.decode(s) file "c:\python33\lib\json\decoder.py", line 352, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) file "c:\python33\lib\json\decoder.py", line 368, in raw_decode obj, end = self.scan_once(s, idx) valueerror: invalid \escape: line 1 column 11 (char 10) but fails, expected.
i can't see way parse json field should contain single backslash after unescaping has taken place.
how can python deserialize string literal {"a":"val\\ue"} (which valid json) appropriate python representation: {'a': 'val\ue'}?
as aside, doesn't pydev inconsistent representation of string uses. watch window shows double backslashes, tooltip of variable shows quadruple backslashes. assume that's "if type string, you'd have use escape original" representation, it's no means clear.
edit follow on @twalberg's answer...
>>> input={'a':'val\ue'} file "<stdin>", line 1 syntaxerror: (unicode error) 'unicodeescape' codec cant decode bytes in position 3-5: truncated \uxxxx escape >>> input={'a':'val\\ue'} >>> input {'a': 'val\\ue'} >>> json.dumps(input) '{"a": "val\\\\ue"}' >>> json.loads(json.dumps(input)) {'a': 'val\\ue'} >>> json.loads(json.dumps(input))['a'] 'val\\ue'
using json.dumps() see how json represent target string:
>>> orig = { 'a' : 'val\ue' } >>> jstring = json.dumps(orig) >>> print jstring {"a": "val\\ue"} >>> extracted = json.loads(jstring) >>> print extracted {u'a': u'val\\ue'} >>> print extracted['a'] val\ue >>> this in python 2.7.3, though, may partially relevant python 3.x environment. still, don't think json has changed much...
Comments
Post a Comment