6

iterating through JSON results can get quite confusing at times. say I have a functionlike so:

def get_playlist_owner_ids(query):

    results = sp.search(q=query, type='playlist')
    id_ = results['playlists']['items'][0]['owner']['id']
    return (id_)

I can fetch the id_, it works.

but how do I iterate using a for i in x loop so I return ALL ids_?

1
  • 2
    All of which ids? You need to show us the structure of your results data structure (and which level or levels you want the id values from) in order for this to be answerable. Maybe you want [x['owner']['id'] for x in results['playlists']['items']], but that's just a guess. Commented Aug 19, 2016 at 23:36

4 Answers 4

5
results['playlists']['items'][0]['owner']['id']
                              ^___ this is a list index

Thus:

for item in results['playlists']['items']:
    print(item['owner']['id'])

It is often convenient to make intermediate variables in order to keep things more readable.

playlist_items = results['playlists']['items']
for item in playlist_items:
    owner = item['owner']
    print(owner['id'])

This is assuming I have correctly guessed the structure of your object based on only what you have shown. Hopefully, though, these examples give you some better ways of thinking about splitting up complex structures into meaningful chunks.

Sign up to request clarification or add additional context in comments.

1 Comment

you have guessed correctly. although a list comprehension would return the data structure like this: [u'handiofiblood', u'holgar_the_red', u'thedoomlord', u'slokra', u'1121479704', u'joakim56789', u'twiggywasp', u'killnot', u'slashxero', u'sandpapersparkle'], therefore in a more efficient way for further processing, it wouldn't be as much readable. I could also append the results to an empty list. thanks.
0

How about this? You can use generator to achieve your goal

def get_playlist_owner_ids(query):
    results = sp.search(q=query, type='playlist')
    for item in results['playlists']['items']:
        yield item['owner']['id']

1 Comment

Why a generator? The whole structure already appears to be loaded into memory.
0

You could iterate over results['playlists']['items'], or, better yet, use list comprehension:

def get_playlist_owner_ids(query):

    results = sp.search(q=query, type='playlist')
    return [x['owner']['id'] for x in results['playlists']['items']]

Comments

0

In practice your document is something like this:

{ "playlists": {
    "items":[
        {"owner":{"id":"1"},...},                 
        {"owner":{"id":"2"},...},
        {"owner":{"id":"3"},...},
        ...,
}

So you have to loop over the list of items.

You can do something like this

ids = []
items = results['playlists']['items']
for item in items:
    ids.append(item['owner']['id'])

return ids

Or if you want to a one line:

ids = [item['owner']['id'] for owner in results['playlists']['items']]

1 Comment

Your one-liner is not quite right. owner['owner']['id']

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.