Set "NA" if field is empty

Hi folks, I receive data from a node with empty fields in several columns. To work properly further with the data in the workflow the fields should not be empty but set to NA or something like this… Is it possible to set all empty fields to a specific value in more than one column efficiantly in a single step? With IF and Set node it becomes a mass to split the data column wise and remerge it afterwords…

If you say “empty” what do you mean? Do they actually exist and are set to an empty string or do they not exist at all? So if you look in the JSON view do those fields get displayed there?

Hi @jan, both. some exist and they are empty strings "email2": "", and some do not exist in JSON view. Is it possible to create if they do not exist and then fill all empty strings with a defined value in some columns?

You can create a Function Node with the following code:

const requiredKeys = [
  'a',
  'b',
  'c',
];

items.forEach(item => {
  requiredKeys.forEach(key => {
    if ([undefined, ''].includes(item.json[key])) {
      item.json[key] = 'NA';
    }
  });
});

return items;

You just have to change the requiredKeys accordingly.

Here an example workflow:

{
  "nodes": [
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {\n      a: 'a1',\n      b: '',\n    }\n  },\n  {\n    json: {\n      b: 'b2',\n    }\n  }\n];"
      },
      "name": "Mock Data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        500,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "const requiredKeys = [\n  'a',\n  'b',\n  'c',\n];\n\nitems.forEach(item => {\n  requiredKeys.forEach(key => {\n    if ([undefined, ''].includes(item.json[key])) {\n      item.json[key] = 'NA';\n    }\n  });\n});\n\nreturn items;"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        700,
        300
      ]
    }
  ],
  "connections": {
    "Mock Data": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Sorry, was busy the whole day and had no time to check it sooner. Ach @jan, what shall I say… couldn’t be better. Thank you so much for your help. This is exactly what I need! :100: :ok_hand:

Great to hear. Have fun!