Home > Software design >  Retrieve only a top-level array from API response
Retrieve only a top-level array from API response

Time:03-10

I'm running an API request which returns a response that looks like this:

{
  "code": 200,
  "data": {
    "total": 2,
    "modifiedSince": "2021-03-10",
    "limit": 1000,
    "count": 2,
    "bookings": [
        {
            "imsId": "21322003",
            "appointmentStartTime": "09:00",
            "modifiedDateTime": "2022-02-08 16:40",
            "futureBooking": true,
            "appointmentStartDate": "2022-03-30",
            "status": "Waiting for acknowledgement"
        },
        {
            "imsId": "21356003",
            "appointmentStartTime": "11:00",
            "modifiedDateTime": "2022-02-08 16:41",
            "futureBooking": true,
            "appointmentStartDate": "2022-03-31",
            "status": "Waiting for acknowledgement"
        }
    ]
  },
  "success": true,
  "requestId": "ab2e4dfd-107d-4c54-99b1-ee52750278d6",
  "text": "OK"
}

And I need to retrieve only a top-level array of the bookings object that would look like this:

{
        "bookings": [
        {
            "imsId": "21322003",
            "appointmentStartTime": "09:00",
            "modifiedDateTime": "2022-02-08 16:40",
            "futureBooking": true,
            "appointmentStartDate": "2022-03-30",
            "status": "Waiting for acknowledgement"
        },
        {
            "imsId": "21356003",
            "appointmentStartTime": "11:00",
            "modifiedDateTime": "2022-02-08 16:41",
            "futureBooking": true,
            "appointmentStartDate": "2022-03-31",
            "status": "Waiting for acknowledgement"
        }
    ]
}

}

Any idea how to do this?

This is my JS request:

  url: 'https://api-au.ims.online/gateway/uat-booking-management/2.0/bookings',
  method: 'GET',
  headers: {
    'Accept': 'application/json',
    'ClientName': 'IMSDEMO',
    'Authorization': `Bearer ${bundle.authData.access_token}`
  },
  params: {

  }
}

return z.request(options)
  .then((response) => {
    response.throwForStatus();
    const results = response.json;

    // You can do any parsing you need for results here before returning them

    return results;
  });

Let me know if you have any ideas. I tried response.bookings in the request and some other combinations but no luck.

CodePudding user response:

have you tried response.data.bookings ?

CodePudding user response:

You can create your own object, for example:

const original = {
  "code": 200,
  "data": {
    "total": 2,
    "modifiedSince": "2021-03-10",
    "limit": 1000,
    "count": 2,
    "bookings": [
        {
            "imsId": "21322003",
            "appointmentStartTime": "09:00",
            "modifiedDateTime": "2022-02-08 16:40",
            "futureBooking": true,
            "appointmentStartDate": "2022-03-30",
            "status": "Waiting for acknowledgement"
        },
        {
            "imsId": "21356003",
            "appointmentStartTime": "11:00",
            "modifiedDateTime": "2022-02-08 16:41",
            "futureBooking": true,
            "appointmentStartDate": "2022-03-31",
            "status": "Waiting for acknowledgement"
        }
    ]
  },
  "success": true,
  "requestId": "ab2e4dfd-107d-4c54-99b1-ee52750278d6",
  "text": "OK"
}

console.log({bookings: original.data.bookings})

So in your code, change

return results;

with:

return ({bookings: results.data.bookings})
  • Related