Search code examples
pythoncurlpython-requestslibcurlpycurl

Extract Id's from response.text()


I want to extract all the values of id's from the following response (finding).

code:

findings= requests.get('https://api.probely.com/targets/TaargetID/findings/36',headers={"Content-Type": "application/json", "Authorization": "JWT <Token>"})
finding=findings.json()

finding:

{"count":45,
   "page_total":5,
   "page":1,
   "length":10,
   "results":[
      {
         "id":79,
         "target":
             {"id":"RzXFSNHH3qUY","name":"","site": 
                 {"id":"2qk21XKLrKyf","url":"https://test- 
                 0.ox.qa.prbly.win",.....},
          "(...)"
         ,
         "id":12,
         "target":
          "(...)"
         ,
         "id":32,
         "target":
          "(...)"
         ,"(....)"
}
]
}

I want to get an array of id's like this: id=[79,12,32] How can I extract these Id's and store in an array?


Solution

  • You can use iteration to go over each item in "results", then save the IDs to an array.

    id_list = []
    
    for result in finding["results"]:
        id_list.append(result["id"])
    

    I'd also like to point out that you shouldn't call any variable id as it is a built in Python function, overwriting it could have bad consequences.