Best answer

Data returned from API call is an object, not an array error


Userlevel 2

I am working to build a custom app integration in Zapier. The API I am working with returns results as an object, not an array like Zapier expects. 

 

I am using Code Mode in Zapier. Here is my code:

 

const options = {
url: `https://myfakeurl.com/${bundle.inputData.PartnerID}`,
method: 'GET',
headers: {
'X-PARTNER-ID': bundle.inputData.PartnerID
},
params: {
'PartnerID': bundle.inputData.PartnerID
}
};

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

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

return results;

});

I get the results from this API call in Postman as follows:

 

{
"ID": "1234567",
"Prefix": "",
"FirstName": "John",
"MiddleName": "",
"LastName": "Doe",
"Suffix": "",
"Salutation": "",
"Spouse": "",
"SpouseLastName": "",
"Company": "",
"Address1": "123 Main Street",
"Address2": "",
"City": "San Diego",
"StateProv": "CA",
"PostalCode": "90210-3438",
"CountryCode": "US",
"PartnerPhones": [],
"PartnerAskArrays": [],
"Email": "test@test.com",
"EmailPreference": "",
"WebURL": "",
"BirthDate": "1976-06-22T00:00:00",
"WeddingDate": null,
"MemberDate": "2020-04-25T03:08:31.493",
"OriginSource": "Online",
"RequiresQC": false,
"Deceased": false,
"NoResponseLetters": false,
"NoDirectMail": false,
"NoTaxStatements": false,
"NoPhoneSolicitation": false,
"NoEmailSolicitation": false,
"UndeliverableAddress": false,
"NoMerge": false,
"VIP": false,
"Prospect": false,
"UndeliverableEmail": false,
"NoResponseEmails": false,
"NoAddressStandardization": false,
"WebUsername": "",
"WebPassword": "",
"WebMustChangePassword": false,
"WebPasswordRecoveryToken": null,
"WebPasswordTokenExpiration": null,
"CustomFlags": [],
"CustomDataFields": []
}

Zapier gives the error: Invalid API Response: - Results must be an array, got: object, ({"ID":”1234567","Prefix":"","FirstName":"John","M) - Got a result missing the "id" property (“1234567") 

After doing many web searches, I found that the parentheses should be replaced with square brackets to indicate it is an array. Is this correct, and if so how do I go about doing this? I am not very familiar with JavaScript and feel pretty lost. I have tried the replace function, but did not have any luck. Many of my attempts resulted in similar errors indicating that it was now a string, not an object, etc. 

I would appreciate any tips for solving this issue. 

Thank you.

icon

Best answer by ikbelkirasan 8 May 2020, 10:21

@ForYourIT Yeah, I wasn’t notified because when you mentioned, my name didn’t become a link. :)

@colinr You’re welcome and don’t worry, the reason behind those warnings is that the code editor in the Platform UI seems to be old and doesn’t support the spread operator (the 3 dots) which is an ES6 feature.

Replace it with this one and it should remove the warnings:

const options = {
url: `https://myfakeurl.com/${bundle.inputData.PartnerID}`,
method: "GET",
headers: {
"X-PARTNER-ID": bundle.inputData.PartnerID,
},
params: {
PartnerID: bundle.inputData.PartnerID,
},
};

return z.request(options).then((response) => {
response.throwForStatus();
const result = z.JSON.parse(response.content);
result.id = result.ID;
return [result];
});

 

View original

This topic has been closed for comments

15 replies

Userlevel 7
Badge +9

Hey - Quick check without looking to deep into this - Have you tried just sending the object in an array?

so instead of

{}

Send like this 

[{}]
  • That’s the first thing I’d try
Userlevel 7
Badge +7

Hi there @colinr ,

 

I don’t think this is an issue with the array or object. I believe the javascript HTTP request should be just fine working with an object, and Zapier prefers to work with this as well.

Zapier gives the error: Invalid API Response: - Results must be an array, got: object, ({"ID":”1234567","Prefix":"","FirstName":"John","M) - Got a result missing the "id" property (“1234567")

So I am not sure what kind of API you are calling, or what kind of request / respons it expects (this would make it a lot easier), but it looks like this could be a API error. I see you are calling the HTTP with;

 headers: {
'X-PARTNER-ID': bundle.inputData.PartnerID
},
params: {
'PartnerID': bundle.inputData.PartnerID
}

If you are however using the inputData from the code action, you should use;

 headers: {
'X-PARTNER-ID': inputData.PartnerID
},
params: {
'PartnerID': inputData.PartnerID
}

Because of that I think the API expects an ID and you are giving none. Which could cause this.

 

Let me know if this works, or if you have any additional questions.

 

~Bjorn

Userlevel 2

Hi Bjorn & Saastronomical,

 

Bjorn, I tried making the changes you suggested and I receive the following error:

“inputData is not defined What happened (You are seeing this because you are an admin): Executing triggers.get_partner_by_id.operation.perform with bundle inputData is not defined”

 

The API I am using has very poor documentation and I feel is outdated. I do not know what kind of response it expects. There is a code sample in jQuery here but it is Greek to me:

https://uat-connect.aegispremier.com/Help/CodeSample?language=JQuery

 

Saastronomical, I am not seeing where to make that change in the code. I’d appreciate any guidance.

 

Thank you both very much, this API has been very frustrating so far!

Userlevel 2

@ForYourIT 

About your thought: “Because of that I think the API expects an ID and you are giving none. Which could cause this.” 

The ID is being sent as part of the API request. It is the PartnerID that is being searched for and results are at least partially returned as shown in my error message in the original post. The ID for this test run is 1234567, so I’m not sure what the problem is. I’m really hating this API. 

@Saastronomical Saastronomical, I am not seeing where to make the changes you suggested in the code. I’d appreciate any guidance.

Userlevel 7
Badge +7

Sorry, I must have been tired since I just now understood you are creating an app and didn't run some javascript code on a webhook. You should keep those variables “bundle.inputData.PartnerID”.

 

Now I have tried the following code to transfer the json object in an array:

var json_data = {
"ID": "1234567",
"Prefix": "",
"FirstName": "John",
"MiddleName": "",
"LastName": "Doe",
"Suffix": "",
"Salutation": "",
"Spouse": "",
"SpouseLastName": "",
"Company": "",
"Address1": "123 Main Street",
"Address2": "",
"City": "San Diego",
"StateProv": "CA",
"PostalCode": "90210-3438",
"CountryCode": "US",
"PartnerPhones": [],
"PartnerAskArrays": [],
"Email": "test@test.com",
"EmailPreference": "",
"WebURL": "",
"BirthDate": "1976-06-22T00:00:00",
"WeddingDate": null,
"MemberDate": "2020-04-25T03:08:31.493",
"OriginSource": "Online",
"RequiresQC": false,
"Deceased": false,
"NoResponseLetters": false,
"NoDirectMail": false,
"NoTaxStatements": false,
"NoPhoneSolicitation": false,
"NoEmailSolicitation": false,
"UndeliverableAddress": false,
"NoMerge": false,
"VIP": false,
"Prospect": false,
"UndeliverableEmail": false,
"NoResponseEmails": false,
"NoAddressStandardization": false,
"WebUsername": "",
"WebPassword": "",
"WebMustChangePassword": false,
"WebPasswordRecoveryToken": null,
"WebPasswordTokenExpiration": null,
"CustomFlags": [],
"CustomDataFields": []
};

var result = [];

for(var i in json_data)
result.push([i, json_data [i]]);

output = ({result})

 

Which works, so you could try something like this:

const options = {
url: `https://myfakeurl.com/${bundle.inputData.PartnerID}`,
method: 'GET',
headers: {
'X-PARTNER-ID': bundle.inputData.PartnerID
},
params: {
'PartnerID': bundle.inputData.PartnerID
}
};

return z.request(options)
.then((response) => {
response.throwForStatus();
const json_data = z.JSON.parse(response.content);

var result = [];

for(var i in json_data)
result.push([i, json_data [i]]);

return results;

});

Let me know if this works or you still getting an error.

 

~Bjorn

Userlevel 2

Hi @ForYourIT,

Thank you very much Bjorn, I think we are all tired (I sure am!) :grinning: I tested your code (I did modify the code slightly as some parts used “result” instead of “results”, but I did try what you sent over first). I am getting the error below:


Invalid API Response: - Got a non-object result in the array, expected only objects (["ID",”1234567"]) - Got a result missing the "id" property (["ID",”1234567"]) What happened (You are seeing this because you are an admin): Executing triggers.get_partner_by_id.operation.perform with bundle Invalid API Response: - Got a non-object result in the array, expected only objects (["ID",”1234567"]) - Got a result missing the "id" property (["ID",”1234567"])


I also tested this code in a different trigger I am building with the same basic setup, but instead of searching by PartnerID, it searches by Email address. (The results returned from either search would the the same fields as below.) I received the same error above for the Email test as well.

I don’t know if this helps, but here is a sample response body format for this API call (PartnerID search) from the API documentation: (The first item labeled “ID” is the PartnerID being searched for using the bundle.inputData.PartnerID input field):

{
"ID": "sample string 2",
"Prefix": "sample string 3",
"FirstName": "sample string 4",
"MiddleName": "sample string 5",
"LastName": "sample string 6",
"Suffix": "sample string 7",
"Salutation": "sample string 8",
"Spouse": "sample string 9",
"SpouseLastName": "sample string 10",
"Company": "sample string 11",
"Address1": "sample string 12",
"Address2": "sample string 13",
"City": "sample string 14",
"StateProv": "sample string 15",
"PostalCode": "sample string 16",
"CountryCode": "sample string 17",
"PartnerPhones": [
{
"Type": "sample string 1",
"Priority": 1,
"Number": "sample string 2"
},
{
"Type": "sample string 1",
"Priority": 1,
"Number": "sample string 2"
},
{
"Type": "sample string 1",
"Priority": 1,
"Number": "sample string 2"
}
],
"PartnerAskArrays": [
{
"AskID": 1,
"AskAmount": "sample string 2"
},
{
"AskID": 1,
"AskAmount": "sample string 2"
},
{
"AskID": 1,
"AskAmount": "sample string 2"
}
],
"Email": "sample string 18",
"EmailPreference": "sample string 19",
"WebURL": "sample string 20",
"BirthDate": "2020-05-04T01:35:59.1698854-06:00",
"WeddingDate": "2020-05-04T01:35:59.1698854-06:00",
"MemberDate": "2020-05-04T01:35:59.1698854-06:00",
"OriginSource": "sample string 21",
"RequiresQC": true,
"Deceased": true,
"NoResponseLetters": true,
"NoDirectMail": true,
"NoTaxStatements": true,
"NoPhoneSolicitation": true,
"NoEmailSolicitation": true,
"UndeliverableAddress": true,
"NoMerge": true,
"VIP": true,
"Prospect": true,
"UndeliverableEmail": true,
"NoResponseEmails": true,
"NoAddressStandardization": true,
"WebUsername": "sample string 36",
"WebPassword": "sample string 37",
"WebMustChangePassword": true,
"WebPasswordRecoveryToken": "3a809c50-8130-4af4-ae69-cdff312da4e3",
"WebPasswordTokenExpiration": "2020-05-04T01:35:59.1698854-06:00",
"CustomFlags": [
{
"FlagName": "sample string 1",
"StartDate": "2020-05-04T01:35:59.1698854-06:00",
"ExpireDate": "2020-05-04T01:35:59.1698854-06:00"
},
{
"FlagName": "sample string 1",
"StartDate": "2020-05-04T01:35:59.1698854-06:00",
"ExpireDate": "2020-05-04T01:35:59.1698854-06:00"
},
{
"FlagName": "sample string 1",
"StartDate": "2020-05-04T01:35:59.1698854-06:00",
"ExpireDate": "2020-05-04T01:35:59.1698854-06:00"
}
],
"CustomDataFields": [
{
"FieldName": "sample string 1",
"FieldValue": "sample string 2",
"FieldType": "sample string 3"
},
{
"FieldName": "sample string 1",
"FieldValue": "sample string 2",
"FieldType": "sample string 3"
},
{
"FieldName": "sample string 1",
"FieldValue": "sample string 2",
"FieldType": "sample string 3"
}
]
}

I found this info doing a web search on the error message, but I’m afraid I am not grasping the solution:

https://zapier.com/developer/documentation/v2/ZDE009/

 

Thank you again for your help, Bjorn, I owe you a few beers!!

Userlevel 7
Badge +7

So I haven’t made a custom Zapier integration before, but mainly worked with AWS Lambda / Fargate and then used a webhook but this will be interesting to take a look at. Anyway, we are having a new error so we are getting somewhere. Here is what I see:

  • Now we are returning an array with strings (["ID",”1234567"]), however, it is expecting objects within an array (["ID": ”1234567"]) 
  • We do not have the specific key that Zapier expects called “id”, which should be unique and always present. You can read more about that here (same as what you found).

Now bear with me, I am no developer...I got a team for that haha. However, I will use all my knowledge to help you out.

Could you try one one of these?

const options = {
url: `https://myfakeurl.com/${bundle.inputData.PartnerID}`,
method: 'GET',
headers: {
'X-PARTNER-ID': bundle.inputData.PartnerID
},
params: {
'PartnerID': bundle.inputData.PartnerID
}
};


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

const requests = results.map((obj) => {
obj.id = obj.ID;
});

return requests;
});


//OR THIS



return z.request(options).then((response) => {
response.throwForStatus();
let items = response.json;

items.forEach(item => {
item.id = item.ID;
})

return items;
});

If this doesn’t work, I know @ikbelkirasan could probably give some valuable input.

 

Cheers!

~ Bjorn 

Userlevel 2

@ForYourIT 

 

Hi Bjorn, I really appreciate your advice and input!! You are closer to being a developer than I am :)

 

Here are the results from testing the code changes you sent over.

This code

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

const requests = results.map((obj) => {
obj.id = obj.ID;
});

return requests;
});

Gives the following error:

results.map is not a function What happened (You are seeing this because you are an admin): Starting GET request to https://myfakeurl.com/api/partner/ID/1234567 Received 200 code from https://myfakeurl.com/api/partner/ID/1234567?PartnerID=1234567 after 830ms Received content "{"ID":"1234567","Prefix":"","FirstName":"John","MiddleName":"","LastName":"Doe","Suffix":"","Salut" results.map is not a function

 

And this code:

 

return z.request(options).then((response) => {
response.throwForStatus();
let items = response.json;

items.forEach(item => {
item.id = item.ID;
})

return items;
});

Gives the following error:

 

items.forEach is not a function What happened (You are seeing this because you are an admin): Starting GET request to https://myfakeurl.com/api/partner/ID/1234567 Received 200 code from https://myfakeurl.com/api/partner/ID/1234567?PartnerID=1234567 after 860ms Received content "{"ID”:”1234567”,”Prefix":"","FirstName”:”John”,”MiddleName":"","LastName”:”Doe”,”Suffix":"","Salut" items.forEach is not a function 

 

I’d appreciate any input as to what the issue might be, Bjorn suggested @ikbelkirasan but I’m not sure how to ask them to take a look. 

 

Best Regards!

Userlevel 7
Badge +11

Hi @colinr and @ForYourIT - Just saw my name by chance lol - I didn’t get any notification!

So I don’t know if you got the issue fixed already or not. The error mentioned that the response must be an array. So, since the API response is a single object, wrapping it in an array should be the way to go.

const options = {
url: `https://myfakeurl.com/${bundle.inputData.PartnerID}`,
method: "GET",
headers: {
"X-PARTNER-ID": bundle.inputData.PartnerID,
},
params: {
PartnerID: bundle.inputData.PartnerID,
},
};

return z.request(options).then((response) => {
response.throwForStatus();
const result = z.JSON.parse(response.content);
return [result];
});

 

Userlevel 2

Hi @ikbelkirasan,

Thank you for the response! I tried the code you gave and got this error:

 

Invalid API Response: - Got a result missing the "id" property ({"ID":"12334567","Prefix":"","FirstName":"John","MiddleName":"","LastName":"Doe","Suffix":"","Salutation":"","Spouse":"","SpouseLastName":"","Company":"","Address1":"3432 Arnold Ave","Address2":"","City":"San Diego","StateProv":"CA","PostalCode":"9210) What happened (You are seeing this because you are an admin): Executing triggers.get_partner_by_id.operation.perform with bundle Invalid API Response: - Got a result missing the "id" property ({"ID":"1234567","Prefix":"","FirstName":"John","MiddleName":"","LastName":"Doe","Suffix":"","Salutation":"","Spouse":"","SpouseLastName":"","Company":"","Address1":"3432 Arnold Ave","Address2":"","City":"San Diego","StateProv":"CA","PostalCode":"9210) Console logs: { ID: '1234567', Prefix: '', FirstName: 'John', MiddleName: '', LastName: 'Doe', Suffix: '', Salutation: '', Spouse: '', SpouseLastName: '', Company: '', Address1: '3432 Arnold Ave', Address2: '', City: 'San Diego', StateProv: 'CA', PostalCode: '92104-3438', CountryCode: 'US', PartnerPhones: [], PartnerAskArrays: [], Email: 'test@test.com', EmailPreference: '', WebURL: '', BirthDate: '1976-06-22T00:00:00', WeddingDate: null, MemberDate: '2020-04-25T03:08:31.493', OriginSource: 'Online', RequiresQC: false, Deceased: false, NoResponseLetters: false, NoDirectMail: false, NoTaxStatements: false, NoPhoneSolicitation: false, NoEmailSolicitation: false, UndeliverableAddress: false, NoMerge: false, VIP: false, Prospect: false, UndeliverableEmail: false, NoResponseEmails: false, NoAddressStandardization: false, WebUsername: '', WebPassword: '', WebMustChangePassword: false, WebPasswordRecoveryToken: null, WebPasswordTokenExpiration: null, CustomFlags: [], CustomDataFields: [] }

 

I received this error before and tried to address it with the code below (from @ForYourIT) but it returned an error. 

 

return z.request(options).then((response) => {
response.throwForStatus();
let items = response.json;

items.forEach(item => {
item.id = item.ID;
})

return items;
});

I received this error:

items.forEach is not a function What happened (You are seeing this because you are an admin): Starting GET request to https://myfakeurl.com/api/partner/ID/1234567 Received 200 code from https://myfakeurl.com/api/partner/ID/1234567?PartnerID=1234567 after 860ms Received content "{"ID”:”1234567”,”Prefix":"","FirstName”:”John”,”MiddleName":"","LastName”:”Doe”,”Suffix":"","Salut" items.forEach is not a function 

 

I’ve also tried the code

 

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

const requests = results.map((obj) => {
obj.id = obj.ID;
});
z.console.log(results);
return requests;
});

But received this error:

results.map is not a function What happened (You are seeing this because you are an admin): Starting GET request to https://uat-connect.aegispremier.com/api/F1331B55-403C-4C15-957D-C4C663E1ABB9/partner/ID/7417451 Received 200 code from https://uat-connect.aegispremier.com/api/F1331B55-403C-4C15-957D-C4C663E1ABB9/partner/ID/7417451?PartnerID=7417451 after 830ms Received content "{"ID":"7417451","Prefix":"","FirstName":"Colin","MiddleName":"","LastName":"Reed","Suffix":"","Salut" results.map is not a function
 

I’m not sure how to address these problems! Thank you very much for everyone’s help with solving this.

 

Userlevel 7
Badge +11

@colinr This should fix the problem.

const options = {
url: `https://myfakeurl.com/${bundle.inputData.PartnerID}`,
method: "GET",
headers: {
"X-PARTNER-ID": bundle.inputData.PartnerID,
},
params: {
PartnerID: bundle.inputData.PartnerID,
},
};

return z.request(options).then((response) => {
response.throwForStatus();
const result = z.JSON.parse(response.content);
return [
{
...result,
id: result.ID,
},
];
});

 

Userlevel 2

@ikbelkirasan Wonderful! This request was successful, yay! Although in code mode, Zapier marked some lines with a red X. Do you think this will cause issues further down the line? I have attached screenshots below.

 

Thank you so very much for your help, it was driving me nuts! :grinning:

 

 

Userlevel 7
Badge +7

Haha thanks @ikbelkirasan ! Weird you didn’t get a notification. Good you could help out.

Great you got it solved @colinr. I think those are just some warnings / issues with indention. But maybe @ikbelkirasan got a better answer for this

Userlevel 7
Badge +11

@ForYourIT Yeah, I wasn’t notified because when you mentioned, my name didn’t become a link. :)

@colinr You’re welcome and don’t worry, the reason behind those warnings is that the code editor in the Platform UI seems to be old and doesn’t support the spread operator (the 3 dots) which is an ES6 feature.

Replace it with this one and it should remove the warnings:

const options = {
url: `https://myfakeurl.com/${bundle.inputData.PartnerID}`,
method: "GET",
headers: {
"X-PARTNER-ID": bundle.inputData.PartnerID,
},
params: {
PartnerID: bundle.inputData.PartnerID,
},
};

return z.request(options).then((response) => {
response.throwForStatus();
const result = z.JSON.parse(response.content);
result.id = result.ID;
return [result];
});

 

Userlevel 2

@ikbelkirasan Excellent, thank you very much! The latest code runs without a hitch and no Zapier warnings either. 

I appreciate everyone’s help on this, now on the the next API call :)