Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

json[more levels] to dict python

i'm having trouble with a simple question.

      a = {
          "apiVersion": "2.1",
          "data": {
               "startIndex": 1,
               "items": [{
                    "id": "YVA3UoZM0zU",
                    "title": "Trailer - Lisbela eo Prisioneiro"          
                        }]
                   }
       }

i don't know how to get the info id. this is a string. so, i tried to make this

           import simplejson as json
           >>> type(js)
           <type 'dict'>
           js = json.loads(a)
           print js['data'{'items'[{'id'}]}]
           >>> syntax error

this syntax is invalid, how could I get this info? it's supposed to be easy. where I'm making wrong?

like image 670
cleliodpaula Avatar asked Mar 25 '26 11:03

cleliodpaula


1 Answers

Try:

js['data']['items'][0]['id']

It would appear that there may be multiple items in this structure. If you'd like to extract all item ids as a list, the following will do it:

[item['id'] for item in js['data']['items']]
like image 189
NPE Avatar answered Mar 28 '26 02:03

NPE



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!