0

I am trying to run an API with multiple id's and I am normalizing the json file and trying it to put into a dataframe. With my current code I am able to get only one response into jsonfile2. When I try to print the json response I will get all the response but response.json() is not working in my code.

for id in ids:
    time.sleep(1)
    url = "https:url/../details/"+str(id)
    querystring = {"API-Token":"Token"}
    headers = {
    'cache-control': "no-cache",
    'postman-token': "111111111111111111111"
    }
    response = requests.request("GET", url.format(id=ids), headers=headers, params=querystring)
#    print(response.json())
    jsonfile2 = response.json()
    works_data2 = json_normalize(jsonfile2, record_path='result')

Example of JSON response

{
  "result": {
    "id": "string",
    "startTime": 0,
    "endTime": 0,
    "tags": [
      {
        "context": "context",
      }
    ],
    "Events": [
      {
        "startTime": 0,
        "endTime": 0,
        "entityId": "string",
        "status": "CLOSED",
        "severities": [
          {
            "context": "context",
            "value": 0,
            "unit": "Bit (bit)"
          }
        ]
}
}

1 Answer 1

2

Right now, you are overwriting the jsonfile2 variable during each iteration of your loop. How about appending to a list instead?

jsonfile2  = []

for id in ids:
    time.sleep(1)
    url = "https:url/../details/"+str(id)
    querystring = {"API-Token":"Token"}
    headers = {
    'cache-control': "no-cache",
    'postman-token': "111111111111111111111"
    }
    response = requests.request("GET", url.format(id=ids), headers=headers, params=querystring)
#    print(response.json())
    jsonfile2.append(response.json())
    works_data2 = json_normalize(jsonfile2, record_path='result')
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @JacobIRR that working and additional I stored the appended data into a dataframe by using the following code works_data2 = json_normalize(jsonfile2)

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.