0

This is my json result from the request

{
"name": "North America",
"region_tag": "na1",
"hostname": "prod.na1.lol.riotgames.com",
"services": [
    {
        "status": "online",
        "incidents": [
            {
                "active": true,
                "created_at": "2018-03-22T10:53:03.397Z",
                "id": 7636
            }
        ]
    }
]

}

I want to print out the value of status from services

My code

    link = "https://na1.api.riotgames.com/lol/status/v3/shard-data?api_key={}".format(API_KEY)
    rq_link=rq.get(link).text
    rq_json=json.loads(rq_link)

I've tried

print(rq_json['services']['status])

and I get an error saying TypeError: list indices must be integers or slices, not str

Is there another way to do it other than using the for loop

for post in rq_json['services']: print(rq_json['status'])

2 Answers 2

1

The answer you're looking for its

rq_json['services'][0]['status']

Because, the service part is described in a list, you need to specify an index to get into inner dict

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

Comments

1

You can use list comprehension:

[print(service['status']) for service in rq_json['services']]

More info about list comprehension could be found in the official Python documentation

Comments

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.