0

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?

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

That is quite strange, I got the information from an API as json after which I stored it in an SQLite database using the Peewee ORM. I then retrieved it from the db using Peewee again, so I guess it should still be json, but then converted to Unicode by the peewee ORM. But thanks for the solution anyway!
0

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>)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.