'json.loads() decodes only with raw string literal
I am trying to execute the following code:
str_2_load='{"source":"\u003ca href=\"http:\/\/twitter.com\" \u003eTwitter \u003c\/a\u003e"}'
json.loads(str_2_load)
Getting the following error:
File "C:\Python27\lib\json\decoder.py", line 381, in raw_decode obj, end = self.scan_once(s, idx) ValueError: Expecting , delimiter: line 1 column 26 (char 25)
But if I use the raw string literal, it works fine. i.e.
str_2_load=r'{"source":"\u003ca href=\"http:\/\/twitter.com\" \u003eTwitter \u003c\/a\u003e"}'
json.loads(str_2_load)
Since the str_2_load
is dynamically assigned, I am looking for a fix for the above issue.
Solution 1:[1]
replace all '\' with '\\':
>>> str_2_load='{"source":"\\u003ca href=\\"http:\\/\\/twitter.com\\" \\u003eTwitter \\u003c\\/a\\u003e"}'
>>> json.loads(str_2_load)
{u'source': u'<a href="http://twitter.com" >Twitter </a>'}
Solution 2:[2]
I believe if you make str_2_load a raw string by adding a 'r' in the beginning of the string. Somehow this is related to the regex.
str_2_load=r'{"source":"\u003ca href=\"http:\/\/twitter.com\" \u003eTwitter \u003c\/a\u003e"}'
json.loads(str_2_load)
#output {'source': '<a href="http://twitter.com" >Twitter </a>'}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | vroomfondel |
Solution 2 | jimxliu |