Question

Google Local Services API + CRM - Nested Arrays ( API Response Mapping Issue )

  • 15 August 2022
  • 1 reply
  • 143 views

Hi there Zapier Community, 

I’m building a custom Zap to integrate Google Local Service Ads API with our CRM. It appears everything is functioning properly however, I noticed several new leads are not coming through, which appears to be due to the API request sending nested data using the same field values. 

 

Here is an example response: 

 

{
"leadId": "000000000",
"accountId": "000000000",
"businessName": "Acme, Inc",
"leadCreationTimestamp": "2022-08-09T16:21:14Z",
"leadType": "PHONE_CALL",
"leadCategory": "roofer",
"geo": "Miami,Florida,United States",
"phoneLead": {
"consumerPhoneNumber": "+15555555678"
},
"chargeStatus": "NOT_CHARGED",
"currencyCode": "USD",
"timezone": {
"id": "America/New_York"
},
"id": "0000000000"
},
{
"leadId": "000000000",
"accountId": "000000000",
"businessName": "Acme, Inc",
"leadCreationTimestamp": "2022-08-09T16:39:38Z",
"leadType": "MESSAGE",
"leadCategory": "roofer",
"geo": "Miami,Florida,United States",
"messageLead": {
"customerName": "Jane Doe",
"jobType": "attic_venting",
"postalCode": "33066",
"consumerPhoneNumber": "+15555555789"
},
"chargeStatus": "CHARGED",
"currencyCode": "USD",
"timezone": {
"id": "America/New_York"
},
"id": "1111111111"
},
{
"leadId": "000000000",
"accountId": "000000000",
"businessName": "Acme, Inc",
"leadCreationTimestamp": "2022-08-10T19:12:28Z",
"leadType": "PHONE_CALL",
"leadCategory": "window_repair",
"geo": "Miami,Florida,United States",
"phoneLead": {
"chargedCallTimestamp": "2022-08-10T19:12:28Z",
"chargedConnectedCallDurationSeconds": "280s",
"consumerPhoneNumber": "+15555555890"
},
"chargeStatus": "CHARGED",
"currencyCode": "USD",
"timezone": {
"id": "America/New_York"
},
"id": "2222222222"
},

 

The issue I’m running into is when mapping the data to our CRM, the trigger test data will is proving multiple fields for ‘consumerPhoneNumber’ ( based on whether it is a message/phone lead and whether the call connected ).  So we are receiving 

 

 

 

How can I parse the API response to properly map the consumerPhoneNumber when the value changes based on messageLead_consumerPhoneNumber versus phoneLead_consumerPhoneNumber?

 

I understand some basic Javascript but parsing API data is new to me, here is the current code for our API request. Any help would be truly appreciated! 

const options = {
url: 'https://localservices.googleapis.com/v1/detailedLeadReports:search',
method: 'GET',
headers: {
'Authorization': `Bearer ${bundle.authData.access_token}`,
'X-QUERY': bundle.authData.query
},
params: {
'query': 'manager_customer_id:XXXXXXXXX',
'pageSize': '1000'

}
}

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

const lists = results["detailedLeadReports"].map((item) => {
return Object.assign(item, {
id: item["leadId"],
});
});

return lists;
});

 


This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

1 reply

Userlevel 6
Badge +8

Hey there @PointClickDigi,

In your JS code you’re referencing results["detailedLeadReports"], yet I don’t see any detailedLeadReports in the sample response you have at the top of your report. I assume the detailedLeadReports is that response itself?

I’m not sure I completely understand what the problem is or what you’re trying to do, but it seems like sometimes you have phoneLead and other times you have messageLead and in both cases you just want to pull out the consumerPhoneNumber to a top-level key.

If that’s the case, something like this should work, though your results may vary if the assumptions I’m making about the structure of the response aren’t correct all the time

 

const lists = results["detailedLeadReports"].map((item) => {
return Object.assign(item, {
id: item["leadId"],
consumerPhoneNumber: item.phoneLead ? item.phoneLead.consumerPhoneNumber : item.messageLead.consumerPhoneNumber
});
});

Let us know if that works or if you have more questions!