0

I am trying to parse the Google Translation result using simplejson in Python. But I am getting the following Exception.

Traceback (most recent call last):
  File "Translator.py", line 45, in <module>
    main()
  File "Translator.py", line 41, in main
    parse_json(trans_text)
  File "Translator.py", line 29, in parse_json
    json = simplejson.loads(str(trans_text))
  File "/usr/local/lib/python2.6/dist-packages/simplejson-2.1.3-py2.6-linux-i686.egg/simplejson/__init__.py", line 385, in loads
    return _default_decoder.decode(s)
  File "/usr/local/lib/python2.6/dist-packages/simplejson-2.1.3-py2.6-linux-i686.egg/simplejson/decoder.py", line 402, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/local/lib/python2.6/dist-packages/simplejson-2.1.3-py2.6-linux-i686.egg/simplejson/decoder.py", line 418, in raw_decode
    obj, end = self.scan_once(s, idx)
simplejson.decoder.JSONDecodeError: Expecting property name: line 1 column 1 (char 1)

This is my json object looks like

{'translations': [{'translatedText': 'fleur'}, {'translatedText': 'voiture'}]}

could anyone tell me what is the problem here?

4
  • There is no such thing as a "json object". You start with a string. Show us the result of repr(trans_text). What do the Google Translate API docs say that trans_text is: str? unicode? something else? Why do you think that you need to do str(trans_text)? Commented Apr 12, 2011 at 7:18
  • when I try to parse with simplejson, it throws the error says it should be either string or stream. I don't know why it is not consider the object as string. hence i need to str() of it. Commented Apr 14, 2011 at 5:49
  • This is my repr of trans text {'translations': [{'translatedText': 'hola'}]} Commented Apr 14, 2011 at 5:51
  • It doesn't consider the object to be a string because (if your repr() has been copied/pasted properly) it is NOT a string, it is a dictionary. Try print type(trans_text). Commented Apr 14, 2011 at 7:45

3 Answers 3

4

You are doing simplejson.loads(str(trans_text))

trans_text is NOT a string (str or unicode) or buffer object. This is witnessed by the simplejson error message, and your report of repr(trans_text):

This is my repr of trans text {'translations': [{'translatedText': 'hola'}]}

trans_text is a dictionary.

If you want to convert that into a JSON string, you need to use simplejson.dumps(), not simplejson.loads().

If you want to use the result for something else, you just need to dig the data out e.g.

# Your other example
trans_text = {'translations': [{'translatedText': 'fleur'}, {'translatedText': 'voiture'}]} 
for x in trans_text['translations']:
    print "chunk of translated text:", x['translatedText']
Sign up to request clarification or add additional context in comments.

Comments

4

The issue is that simplejson supports json with double quote encoded strings and not single quote encoded strings, so a naive solution might be

json.loads(jsonstring.replace("'", '"'))

3 Comments

-1 That is a horribly naive solution to what may not even be the problem! It is addressing the symptoms, when the root cause for Google ostensibly emitting a JSON string with the wrong quotes has not been investigated.
This solution causes the other problems like if the translation content has the single quote the replace all changed that too double quote and its meaning changed and also gives parser exception. Any more thought in it how to fix this issue?
@Ananth Duari: see my answer.
2

JSON syntax doesn’t support full syntax of JavaScript. Unlike JavaScript, JSON strings and attribute names have to be double-quoted.

string ::= "" | " chars "

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.