Is there a better way to accomplish this task?

Adding a variable with one of three values (more in the future)

I updating contacts on Google using the People API. I have created the JSON for the data, and I’m working on the last bits. Specifically the updateMask variable. This is supposed to contain the names of arrays we’re updating. Right now, I’m only working on Email addresses and Phone Numbers, However, this could quickly get out of control if there are more than a handful of possible entries.

My desired outcome is to have “updateMask” updated to:

“emailAddresses” if there are email addresses to update
“phoneNumbers” if there are phone numbers to update
“emailAddresses,phoneNumbers” if both arrays exist.

What I have works, but if there are ten possibles instead of two, this will get out of control.

No error. I’m just verifying this is as intended.

Please share your workflow

Share the output returned by the last node

One or more JSON objects:

{
“updateContact”: {
“contact”: {
“emailAddresses”: [
{
“value”: “[email protected]”,
“type”: “work”
}
],
“phoneNumbers”: [
{
“value”: “(503) 450-0900”,
“type”: “home”
}
]
},
“resourceName”: “people/c5790027538480083194”,
“id”: 334,
“updateMask”: “phoneNumbers”
}
}

Information on your n8n setup

  • n8n version: 1.50.1
  • Database (default: SQLite): postgresQL
  • n8n EXECUTIONS_PROCESS setting (default: own, main): Main
  • Running n8n via (Docker, npm, n8n cloud, desktop app): Docker on DO
  • Operating system: Ubuntu 22.04.4 LTS

@russellkg , I believe below is a more elegant way of composing the mask. It should work regardless of how many names of arrays you have. Basically, you just need to apply the expression {{ Object.keys($json.updateContact.contact).join() }}.

2 Likes

Thank you so much!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.