I’m trying to detect if a field exists in JSON received from a webhook inside a “Code node” and based on the result call another node, is that possible? or is there another way to do that with another node? I looked at the documentation and the community but I didn’t find a way to achieve this.
What is the error message (if any)?
Please share your workflow
This is the logic I have with a script:
const jsonData = {
// the webhook received JSON data here
};
// Check if "uuid" exists in "meta_data"
const metaData = jsonData.meta_data || [];
let uuidExists = false;
for (const item of metaData) {
if (item.key === "uuid") {
uuidExists = true;
break;
}
}
if (uuidExists) {
call or continue with "send email node" for example
} else {
stop and do nothing node
}
Share the output returned by the last node
Information on your n8n setup
n8n version: 1.1.1
Database (default: SQLite): Postgre SQL
n8n EXECUTIONS_PROCESS setting (default: own, main): own
Running n8n via (Docker, npm, n8n cloud, desktop app): Docker
Hi @rodmontgt, this isn’t directly possible but you could consider returning your uuidExists field through your Code node, then connect a subsequent If node. If uuidExists is true, run your other node, otherwise don’t.
Thanks @MutedJam, following your advice, I was able to accomplish it with the following code:
// Check if UUID field exists
const jsonData = $('Order').first().json; // the webhook received JSON data here
// Check if "uuid" exists in "meta_data"
const metaData = jsonData.order.body.meta_data || [];
let uuidExists = 0; // Initialize with false value
let uuidValue = null; // Initialize the variable to store UUID value
for (const item of metaData) {
if (item.key === "uuid") {
uuidExists = 1; // Assign 1 if positive, 0 if false
uuidValue = item.value; // Store the UUID value
break;
}
}
//Return UUID value
return [{
json: {
"uuidValue": uuidValue,
"uuidExists": uuidExists
}}
];
Then with a IF node, I can compare the uuidExists value, I wanted to do it with boolean values but I have no luck with that, so with numbers is OK