7

I'm new to Python and JSON, so I'm sorry if I sound clueless. I'm getting the following result from the Google Translate API and want to parse out the value of "translatedText":

{
 "data": {
  "translations": [
   {
    "translatedText": "Toute votre base sont appartiennent à nous"
   }
  ]
 }
}

This response is simply stored as a string using this:

response = urllib2.urlopen(translateUrl)
translateResponse = response.read()

So yeah, all I want to do is get the translated text and store it in a variable. I've searched the Python Docs but it seems so confusing and doesn't seem to consider JSON stored as a simple string rather than some super cool JSON object.

2
  • 3
    In the interests of improving the docs: Did you find the json module in the docs? Did you find the "Basic Usage" section? What was there about the description of dumps and loads that gave you the impression that the docs didn't "seem to consider JSON stored as a simple string rather than some super cool JSON object"? Would it have been more helpful if the examples were included with each function definition instead of in one big bunch up the front? Commented Jan 15, 2011 at 3:04
  • 1
    Well I'm new to the Python docs in general so it's not clear to me how things are described. In the PHP/Java docs, functions seem to be much clearly organised and defined, but this is probably just what I'm used to. Also yes, I think it would be better if the examples were given with the functions instead of at the start because it's not clear what they're actually doing until you read ahead. I can see now what the loads function does, but when I was scanning it earlier it looked like it was taking in a special format rather than a JSON formatted string. Commented Jan 15, 2011 at 13:12

1 Answer 1

13

You can parse the text into an object using the json module in Python >= 2.6:

>>> import json
>>> translation = json.loads("""{
...  "data": {
...   "translations": [
...    {
...     "translatedText": "Toute votre base sont appartiennent  nous"
...    },
...    {
...     "translate": "¡Qué bien!"
...    }
...   ]
...  }
... }
... """)
>>> translation
{u'data': {u'translations': [{u'translatedText': u'Toute votre base sont appartiennent  nous'}]}}
>>> translation[u'data'][u'translations'][0][u'translatedText']
u'Toute votre base sont appartiennent  nous'
>>> translation[u'data'][u'translations'][1][u'translate']
u'¡Qué bien!'
Sign up to request clarification or add additional context in comments.

2 Comments

If you have python <= 2.6 you can use simplejson package. It's the same that json, but not in python standard library.
and yeah, please consider the edits I have suggested..they clear some doubts that I still had after reading your answer

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.