0

I'm trying to retrieve only requestId object from json results after querying my API.

my code:

def findRequests(group, token, user):
    headers = { 'accept': 'application/json', 
        'Content-Type': 'application/json', 
        'token': token,
        'user': user}
    endpoint = 'requests/find'
    body = {'groupIDs': [group], "createdDate": {'operator': "BETWEEN", 'fromDate': "01-APR-2020 00:00:00", 'toDate': "21-APR-2020 00:00:00"}} 
    r = requests.post(url = host + endpoint, headers = headers, json=body, verify=False)
    data = json.loads(r.text)
    print (data[0]['requestId'])

json data:

[{'requestId': 2567887, 'requestName': 'Here is a sample name', 'requestSubject': 'sample subject', 'createdDate': '01-APR-2020 14:06:03'}, {'requestId': 7665432,...}] 

then I would like to save all the values for requestId object found in results as a list:

 myRequestsList = print(findRequests(group, token, user))

However the above will only return a single requestId and not all of the ones that were returned in data variable in def findRequests(group, token, user). What am I doing wrong?

Output:

2567887
None

Desired output:

2567887,
7665432

Could someone help me with this? thanks in advance!

2
  • Your function should return not print Commented Apr 20, 2020 at 16:23
  • just changed to return (data[0]['requestId']) but when trying myList = print(findRequests(group, token, user)) afterwards it still returns only a single Id and not all of the Ids found in json data. Is there something else Im doing wrong? Commented Apr 20, 2020 at 16:31

1 Answer 1

1

First, you should modify your func:

Then, assign the variable to the func, not the print:

myRequestsList = list(findRequests(group, token, user)))

(!) However, I assume that group,token, user are replaced by other variables.

And finally, to get the output:

for req in myRequestsList:
 print(req)

Later edit:

def findRequests(group, token, user):
    headers = { 'accept': 'application/json', 
        'Content-Type': 'application/json', 
        'token': token,
        'user': user}
    endpoint = 'requests/find'
    body = {'groupIDs': [group], "createdDate": {'operator': "BETWEEN", 'fromDate': "01-APR-2020 00:00:00", 'toDate': "21-APR-2020 00:00:00"}} 
    r = requests.post(url = host + endpoint, headers = headers, json=body, verify=False)
    data = json.loads(r.text)
    final_list = []
    for each_req in data:
      final_list.append(each_req['requestId'])
    return final_list

myRequestsList = findRequests(group, token, user)

for each in myRequestsList:
 print(each)
Sign up to request clarification or add additional context in comments.

10 Comments

I've tried the above and when running this bit myRequestsList = list(findRequests(group, token, user)) it says TypeError: 'int' object is not iterable. Any idea what am I doing wrong here? thanks!
It is because findRequests(group, token, user)) retrieves a integer; do you mind sharing the json or just a small part of it (containing a12bd-fr145-52dg, b67da-pl132-53dg, c78aa-de111-890)
Np about the questions :) My bad, used with` instead of for. modified the func. for each_req in data: instead of with each_req in data. Updated the answer. Sorry again.
It worked now! thanks a lot for helping me with this!
Hi @Baobab1988 Included some samples in repl.it/repls/IdolizedEducatedDecompiler
|

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.