0

I am trying to run a query with an API to get a piece of information. However, I get too much data. I am looking for a way to only get a specific value, which is the number after "totalItemCount". How can I search for the value of "totalItemCount" which shows up like "totalItemCount":number,. How can I run the API query search for number for "totalItemCount"? So far, I have code:

import requests
import json

##headers
headers = {
    'Accept': 'application/json',
    'x-api-key':'apikeyNumber'
}

response = requests.get('https://app.application.com/api/v3/agents/1', headers=headers)

data = response.json()

data_str = json.dumps(data, indent=2)

print(data_str)

Output: Large amount of data, a snippet:

...
      "LastRebootTime": "2022-11-10T15:25:44Z",
      "OSVersion": "21H2",
      "OSBuild": "19044.2251",
      "OfficeFullVersion": "16.0.10391.20029",
    }
  ],
  "totalItemCount": 1088,
  "page": 1,
  "itemsInPage": 20,
  "totalPages": 55,
  "prevLink": "",
}

Not sure on the parsing after running the api query.

0

1 Answer 1

1

The question is unclear as to whether you want to retrieve the data from the parsed json or specialize the request so that only the "totalItemCount": 1088 data are returned. If you want to reduce what comes back in the request then the only way to determine this is to read the documentation for the API you are using. If not there you can try asking on a help forum for the API service-assuming they host one.

If you want to retrieve only the data from the parsed response then you can use the dictionary .get() method. The default for the second argument is None which specifies the value if this key is not present in the response. E.g.

import requests
import json


headers = {
    'Accept': 'application/json',
    'x-api-key':'apikeyNumber'
}


response = requests.get('https://app.application.com/api/v3/agents/1', headers=headers)

data = response.json()
data_str = json.dumps(data, indent=2)
totalItemCount = data_str.get("totalItemCount", 0)

will give you a value in totalItemCount of 0 if the key-value pair is not present for any reason. Otherwise it will return the value present in the response.

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

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.