Help needed to avoid merge executing previous node

Describe the issue/error/question

This might be complicated to explain, but I’ll do my best!

I am trying to process data captured through a form. The form JSON payload may (or may not!) contain responses in some fields.

What I’m currently doing is splitting out the fields that are ‘required’ (I know these will have responses) and those that are optional - using Set, to two different payloads. I have 4x optional attributes. Basically, I want to transform the value of each field in this payload, if it exists. If it does not exist, it should be ignored.

What I currently have (below) works when all fields are populated. When some of these optional fields are empty, the merge will force the previous node to execute so I’ll get the additional transform text that I want, but without the value.

My logic is -

  • If the ‘Company’ field is populated, manipulate it. Otherwise, delete the company field.
    OR
  • If the ‘Phone’ field is populated, manipulate it. Otherwise, delete the phone field.
    OR
    — etc.

I’ve tried to process this linearly - using Set to remove the field that should be vacant, then feeding it to the next condition. The challenge here is that both a true and false condition on ‘company’ would I think need to form the input to the if for ‘phone’… which means that the company field, if previously removed, may get added back.

There has to be a better way to do it, I’m just stuck on how to do it.

I’d love to be able to do this without using ‘Merge’.

What is the error message (if any)?

Please share the workflow

This is the part of the workflow in question.

Share the output returned by the last node

Current output:
[
{
“ps_company”:“-Company “My Company””,
“ps_phone”:“-MobilePhone “””,
“ps_enddate”:“-AccountExpirationDate “,
“ps_strengths”:”-OtherAttributes @{info=“One | Two | Three | Four | Five”}”
}
]

Desired output (“Phone” and “End Date” fields from earlier should not be processed):
[
{
“ps_company”:“-Company “My Company””,
“ps_strengths”:“-OtherAttributes @{info=“One | Two | Three | Four | Five”}”
}
]

Information on your n8n setup

  • n8n version: 0.204.0
  • Database you’re using (default: SQLite): SQLite
  • Running n8n with the execution process [own(default), main]:
  • Running n8n via [Docker, npm, n8n.cloud, desktop app]: Docker

Hey @Tom_Anderson,

Have you thought about using a code node? You could then do everything with a bit of javascript and just have the one node.

1 Like

Hey Jon,

Yeah - could do that. The big issue is… I don’t know javascript (or really any other language for that matter!). I’ll see if I can nut it out.

I’ve spent some time on this and have come up with a function which as far as I can determine, should work fine. But it doesn’t. I did have someone else with more javascript knowledge than myself take a look at it, and they also can’t figure why it doesn’t work.

const jsonData = $input.all()

function removeEmptyKeys(jsonData) {
  for (var key in jsonData) {
    if (jsonData[key] === "") {
    delete jsonData[key];
    }
  }
}

console.log(removeEmptyKeys(jsonData));
return jsonData;

When I pass in the following input, I get identical output (returned and in console.log).

[
  {
    "company": "My Company",
    "phone": "",
    "enddate": "",
    "strengths": "One, Two, Three, Four, Five"
  }
]

What am I missing??? I’m expecting to just have ‘company’ and ‘strengths’ keys returned.

Hey @Tom_Anderson,

Give this a bash.

Thanks for the guidance @Jon

I actually managed to get it working myself yesterday! Took longer than it ought to have but I guess that’s expected when trying to also learn the language!

for (let key in $json) {
    if ($json[key] === "") {
      delete $json[key];
    }
}
return { json : $json };
1 Like

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