0

I am trying to run a GET request to an API that returns JSON. The JSON response looks something like this:

{
    "nodes": [
        {
            "id": "5C73B00598",
            "connectionState": "connected",
            "model": "PP152X"
        },
        {
            "id": "5C73B0059E",
            "connectionState": "connected",
            "model": "PP152X"
        },
        {
            "id": "4C72D012D8",
            "connectionState": "connected",
            "model": "PP203X"
        },
        {
            "id": "5C73B005DE",
            "connectionState": "connected",
            "model": "PP152X"
        },
        {
            "id": "5C73B005A2",
            "connectionState": "connected",
            "model": "PP152X"
        },
        {
            "id": "5C73B004A2",
            "connectionState": "disconnected",
            "model": "PP152X"
        },
        {
            "id": "5C73B0058B",
            "connectionState": "connected",
            "model": "PP152X"
        }
    ]
}

The simplified version of the script to just return one response from one API folder is this so far:

import requests
import json


url = "https://myurl.io/api/Customers/xxx/locations/xxx/nodes"

payload = {}
headers = {
  'Content-Type': 'application/x-www-form-urlencoded',
  'Authorization': 'xxx'
}

response = requests.request("GET", url, headers=headers, data = payload)
str = response.text.strip()



json_str = json.loads(str)
print(len(json_str['nodes']))
i = 0
while i <= len(json_str['nodes']):
    #cus_list = json_str['nodes'][i]['connectionState']
    #cus_lists.append(cus_list)
    print(json_str['nodes'][i]['connectionState'])
    i += 1

When I run this script the while loop does not return 7 iterations of the 'connectionState' but rather just the first one.

2
  • Doesn't answer your question but the condition in your while loop should use <. Also paste the actual output of that print statement and the response, not a truncated version of the output. Commented Apr 16, 2020 at 10:18
  • 1
    the condition aside, the loop seems fine. Are you sure it's returning 7 and not 1 from the api response? Commented Apr 16, 2020 at 10:27

2 Answers 2

2

Try:

for i in json_str['nodes']:
    print(i['connectionState'])

This gives:

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

Comments

0

i must always be smaller then the len()

json_str = json.loads(str)
print(len(json_str['nodes']))
i = 0
while i < len(json_str['nodes']):
    #cus_list = json_str['nodes'][i]['connectionState']
    #cus_lists.append(cus_list)
    print(json_str['nodes'][i]['connectionState'])
    i += 1

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.