I’m working on a Zapier integration and running into an issue with the data format. My API sends back a JSON response that looks like this:
{
status: "OK",
description: "Found 1 reservation record",
reservations: {
123: {
details: {
booking_title: "John's Meeting",
scheduled_date: "Monday 15th Jul 2024"
}
}
}
}
Here’s my Zapier code that processes this data:
const requestConfig = {
url: bundle.authData.endpoint + `/api/v2/reservations`,
method: 'GET',
headers: {
'Content-Type': 'application/json'
},
params: {
'token': bundle.authData.access_token,
'password': bundle.authData.secret_key,
'reservation_id': 123,
'details': 'complete'
},
}
return z.request(requestConfig)
.then((res) => {
res.throwForStatus();
const data = z.JSON.parse(res.content);
return data["reservations"];
});
But I keep getting this error message saying that the trigger needs an array of objects, not the object I’m returning. The error shows the exact object structure I’m sending back. I thought my JSON parsing was correct and that I was returning the right format. What am I missing here?
You’re returning data["reservations"] as an object with numeric keys, but Zapier triggers need an array. Convert the object to an array first:
const reservationsObj = data["reservations"];
const reservationsArray = Object.keys(reservationsObj).map(key => ({
id: key,
...reservationsObj[key]
}));
return reservationsArray;
This turns your object into an array where each reservation becomes an element with the ID as a property. I’ve hit this same issue with API responses before - works great for Zapier integrations. Your trigger will get the array format it expects and stop throwing errors.
zapier triggers need arrays but ur returning an object. quick fix: return [data.reservations['123']] to wrap it in an array. if u want multiple reservations, use Object.entries(data.reservations).map(([id, res]) => ({id, ...res})) to convert the whole object. been there with similar apis lol
The problem is Zapier wants an array for triggers, but your API sends back reservations as an object with keys. I’ve hit this same issue building webhook integrations. Don’t return data["reservations"] directly - you need to transform it first. Try wrapping the object values in an array:
const reservationsData = data["reservations"];
return Object.values(reservationsData).map((reservation, index) => {
const reservationId = Object.keys(reservationsData)[index];
return {
reservation_id: reservationId,
booking_title: reservation.details.booking_title,
scheduled_date: reservation.details.scheduled_date
};
});
This flattens your nested structure into the array format Zapier expects and keeps the reservation ID intact. Should work without any complaints.