abhinav ranish
abhinav ranish

Reputation: 7

Extracting specific JSON values in python

JSON return from spotify api. Example:

{
    "tracks": {
        "href": "https://api.spotify.com/v1/search?query=Stero+Hearts&type=track&offset=0&limit=1",
        "items": [
            {
                "album": {
                    "album_type": "album",
                    "artists": [
                        {
                            "external_urls": {
                                "spotify": "https://open.spotify.com/artist/4IJczjB0fJ04gs4uvP0Fli"
                            },
                            "href": "https://api.spotify.com/v1/artists/4IJczjB0fJ04gs4uvP0Fli",
                            "id": "4IJczjB0fJ04gs4uvP0Fli",
                            "name": "Gym Class Heroes",
                            "type": "artist",
                            "uri": "spotify:artist:4IJczjB0fJ04gs4uvP0Fli"
                        }
                    ]
                }
            }
        ]
    }
}

Broken Code

import requests, json


spotifytrack = input("Name of Song?\\n")
link = "https://api.spotify.com/v1/search?q=" + spotifytrack + "&type=track&limit=1"
token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
header = {
    "Authorization": "Bearer {}".format(token),
    "Content-Type": "application/json",
    "Accept": "application/json",
}
auth_response = requests.get(link, headers=header)
pretty_response = json.dumps(auth_response.json(), indent=4)
data_by_user = {}


for d in auth_response:
    data_by_user[d["artist"]] = d
    print(data_by_user["uri"])


"""
def find_track_from_json(auth_response, artist):
    return [p for p in auth_response if p["artist"] == artist][0]["uri"]


urii = find_track_from_json(auth_response, "uri")
print(urii)
x = load.json(auth_response.json())
print("Here is the data whic we have imported\n")
print(pretty_response)
print(x["name"])
print(x["uri"])
print(x["spotify"])
"""

Errors noticed:

  File "spotify.py", line 19, in <module>
    data_by_user[d["artist"]] = d
TypeError: byte indices must be integers or slices, not str

The aim is to convert word search to link in a cli application.

I tried load.json which i saw in some website and also tried def.

I expected the program to find out the artist name and uri from the json and print it in the cli interface.

Upvotes: 0

Views: 150

Answers (2)

Georgi Goranov
Georgi Goranov

Reputation: 16

The result from requests.get() is a requests.Response object. As far as I can see you want to iterate over the response body which is JSON. The requests.Response object has a .json() method which returns a dict from the response JSON.

Looking at the response you would probably want to iterate over resp_json['tracks']['items'] which is a list.

So to summarize your code should look something like this:

auth_response = requests.get(link, headers=header)
items = auth_response.json()['tracks']['items']
for d in items:
    print(d)

Upvotes: 0

Jim Wright
Jim Wright

Reputation: 6058

You are iterating over the encoded json string:

auth_response = requests.get(link, headers=header)
for d in auth_response:

Python is complaining that you aren't providing a numerical index, which is correct as auth_response is just a string!

You should call json.loads to decode the string, and then you can iterate over it.

auth_response = requests.get(link, headers=header)
decoded_auth_response = json.loads(auth_response)
data_by_user = {}
for d in decoded_auth_response:
    data_by_user[d["artist"]] = d

As you haven't provided the full json output from the API call I'm not sure what data is actually in decoded_auth_response, and you haven't described what your expected output would look like, so you may need to do some more work to find the correct data in each iteration.

Upvotes: 1

Related Questions