Code to remove null values from data

I’m running self-hosted n8n via docker and trying to delete the null values from the input data. I searched the forum and found this, but for the life of me, I can’t get it to work without throwing an error.

for (const key in item) {  
  if(item[key]===null){
    delete item[key];
  }
}
return item;

The input for the code node is:

[
{
"5" - 1st": "80",
"5" - 2nd": null,
"5" - 3rd": null,
"6" - 1st": null,
"6" - 2nd": null,
"6" - 3rd": null,
"Profile": "Base",
"Corners": "2"
}
]

I would like it to just output the non-null values.

Thanks!

Assuming you’re using a Code node to do this, is it in Run Once for Each Item mode?

Also you may want to replace item with $json. If anything in code is marked in red, it’s a source of errors (with a very few exceptions tho).

If this resolves your question, please mark this post as a :white_check_mark: Solution.

1 Like

THANK YOU, THANK YOU, THANK YOU!! That was it!

Glad it worked. Please post a workflow using the </> button next time.
It is not easy to guess what’s going on without seeing the actual workflow and maybe even some data.

Happy n8ning!

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