Reputation: 53873
I've got some simple json formatted in unicode which I want to load using the usual python json.loads():
>>> er.rates
u"{u'sell': u'1.3477', u'buy': u'1.3588'}"
>>> json.loads(er.rates)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 338, in loads
return _default_decoder.decode(s)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 365, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 381, in raw_decode
obj, end = self.scan_once(s, idx)
ValueError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
So I tried using ensure_ascii=False
:
>>> json.loads(er.rates, ensure_ascii=False)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 351, in loads
return cls(encoding=encoding, **kw).decode(s)
TypeError: __init__() got an unexpected keyword argument 'ensure_ascii'
Does anybody know how I can load this unicode json?
Upvotes: 0
Views: 1385
Reputation: 2446
Many API data providers return unicode string which is easily rendered in a browser. Unicode string (even when it looks like json) and json are not the same thing from a 'computer' perspective.
If you have a unicode (json-like) string you should be able to use json.loads(<your unicode json like string>)
Upvotes: 0
Reputation: 599610
That is not json. It is a string representation of a python dict, which is something quite different.
You can use ast.literal_eval
to load it.
Upvotes: 4