0

As a result of an API call I get the following object of <type 'unicode'>:

{"From":"en","Translations":[{"Count":0,"MatchDegree":100,"MatchedOriginalText":"","Rating":5,"TranslatedText":"Cómo estás"}]}

but when I try to parse it with simplejson_loads() I get this error:

simplejson.scanner.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

How can I handle this kind of objects?

EDIT II: the JSON is correct. What messes up things is the BOM at the beginning of the string. Trying to get rid of it with .encode('utf-8-sig') produces the error

UnicodeEncodeError: 'ascii' codec can't encode character u'\ufeff' in position 2: ordinal not in range(128)

but somewhere in this discussion I found a solution that worked for me:

if u.startswith(u'\ufeff'):
  u = u[1:]

And I'm quite tempted to just get away with it and be happy.

Community
  • 1
  • 1
laurids
  • 931
  • 9
  • 24
  • can't reproduce. `import json; json.loads(u'that string')` gives me back the expected `dict`. – roippi May 16 '14 at 23:11
  • @roippi you're right. It looks like an encoding issue. I edited the question with further informations... – laurids May 16 '14 at 23:34

2 Answers2

0

Try to parse just the JSON, whatever is between the very first { and the very last }.

If that is not enough, please give us more details.

kiwixz
  • 1,380
  • 15
  • 23
  • I'm not sure what you mean. I do try to parse the whole thing, that is from the very first { and the very last }, and I reported the error I get. There is nothing but that JSON in the API response. – laurids May 16 '14 at 22:47
0

Try this:

import asp

foo = ast.literal_eval(your_result)

This will convert unicode object to python dictionary, is that your solution?

juree
  • 253
  • 2
  • 12
  • `literal_eval()` works (as well as `json.loads()` ) but only if I remove the BOM first. Turned out that was the source of all problems... – laurids May 17 '14 at 00:15