wonnie
wonnie

Reputation: 479

json decode issue with bing data (python)

I am in trouble with the bing's json api.

Here is the json data i am receiving from api.bing.net/json.aspx:

{"SearchResponse":{"Version":"2.2","Query":{"SearchTerms":"news"},"Translation":{"Results":[{"TranslatedTerm":"Noticias"}]}}}

I need to parse the TranslatedTerm value "Noticias" but it seems i have a problem with the json decode. I am using this..

           result = j.loads(bytes)
            print result['SearchResponse']['Translation']['Results']

And python gives me this:

[{u'TranslatedTerm': u'Noticias'}]

If i add use it like this:

result['SearchResponse']['Translation']['Results']["TranslatedTerm"]

python raises an error like

print result['SearchResponse']['Translation']['Results']["TranslatedTerm"]

TypeError: list indices must be integers

How can i get the 'Noticias' as a plain string? Much appriciated...

Upvotes: 0

Views: 348

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599956

The translation Results is a list - presumably because there can be many results.

If you're sure you're only interested in the first result, you can do this:

result['SearchResponse']['Translation']['Results'][0]['TranslatedTerm']

Upvotes: 3

Related Questions