Enrique Vargas
Enrique Vargas

Reputation: 159

Iterating through Django Rest API and obtaining an error

I have the following Django Rest API structure:

[
   {
        "title": "Project 1",
        "description": "API projects",
        "members": [
            {
                "latest_activity": "15151020",
                "first_name": "AleX",
                "minutes_last_week": 0,
                "last_name": "Mol",
                "id": 23,
                "minutes_total": 30,
                "minutes_today": 0
            },
            {
                "latest_activity": "1515181664",
                "first_name": "Annie",
                "minutes_last_week": 0,
                "last_name": "Az",
                "id": 47,
                "minutes_total": 20,
                "minutes_today": 0
            }
        ]
    },
    {
        "title": "Project 2",
        "description": "Developer test (internal   project",
        "members": [
            {
                "latest_activity": "1511600",
                "first_name": "Ivan",
                "minutes_last_week": 0,
                "last_name": "XJJNX",
                "id": 18,
                "minutes_total": 10,
                "minutes_today": 0
            },
            {
                "latest_activity": "1516985",
                "first_name": "Lauren",
                "minutes_last_week": 0,
                "last_name": "Gom",
                "id": 39,
                "minutes_total": 560,
                "minutes_today": 0
            }
        ]
    }    
]

What I want to do is iterate over this API, and obtain all the project names and developers that have been working in each project, so I developed the following function:

    def execute():
        respuesta = requests.get('http://projd.herokuapp.com/api/v1/activities/?format=json', auth=('xxx','xxx'))
        upresponse = respuesta.json()

        for team in range(0, len(upresponse)-1):upresponse[team]["members"]
            print(team["first_name"]+" "+ team["last_name"] + " has been working in " + team["latest_activity"])

And I'm doing something wrong and I'm obtaining the following error: TypeError: 'int' object is not subscriptable.

I can't understand why.

Upvotes: 1

Views: 1284

Answers (1)

Rakesh
Rakesh

Reputation: 82775

This should help.

for project in upresponse:    #Iterate over projects
    for team in project["members"]:   #Iterate over members
        print(team["first_name"] + " " + team["last_name"] + " has been working in " + team["latest_activity"])

Output:

AleX Mol has been working in 15151020
Annie Az has been working in 1515181664
Ivan XJJNX has been working in 1511600
Lauren Gom has been working in 1516985

Upvotes: 1

Related Questions