Is this possible? Whole string from keyword condition?

Hi,
is this possible? If you have:

{

“CODE”: “123123123A123123123l, 12312B12399123123, 9085A8499, 182399A4747”

},

And only keeping values that have “A”, so only “numbers”: “123123123A123123123l, 9085A8499, 182399A4747”

Would be kept? Thank you.

Check the example below:

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {\n      CODE: \"123123123A123123123l, 12312B12399123123, 9085A8499, 182399A4747\"\n    }\n  }\n]"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        550,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "\nconst parts = []\n\nfor (part of item.CODE.split(',')) {\n    if (part.includes('A')) {\n      parts.push(part)\n    }\n}\n\nitem.newCode = parts.join(',')\n\nreturn item\n\n"
      },
      "name": "FunctionItem",
      "type": "n8n-nodes-base.functionItem",
      "typeVersion": 1,
      "position": [
        810,
        300
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function": {
      "main": [
        [
          {
            "node": "FunctionItem",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Hi @RicardoE105, I am getting this error: ERROR: Cannot read property ‘split’ of undefined.

My “real” structure is like this (Apologies thought taking shortcuts would make things easier):

[
{
“codes”: “123123123D123123123l, 12312B12399123123, 9085M8499, 182399A4747”
},
{
“code2”: “9809751D2C9393, 9128123CD123h21323”
},
{
“code4”: “12723473123A123123123l, 182399A4747, 12312B12399123123”
},
{
“codes”: “9085A8910923, 217631273123123123A123123123l, 12312B12399123123, 9085C123"
},
{
“code3”: “98097512C9393, 9128123123h21323”
},
{
“codes”: “12723473123A123123123l, 182399A4747, 12312B12399123123”
},
{
“code2”: “98097512C9393, 9128123123h21323”
}
]

Ideally I just need to get into the “codes” (Don’t need code2,3,4 and so on) and extract string with “A” condition. Is possible also just to take first “A” string that appears in case there are two “A” in same line?

Final result would be:
[
{
“codes”: “182399A4747"
},
{
“codes”: “217631273123123123A123123123l"
},
{
“codes”: “12723473123A123123123l”
},
]

Thank you very much :slight_smile:

Should not be just codes? Instead of codes1, codes2, etc

Hi @RicardoE105,
Yes, it should just leave “codes” and ignore the others, was just showing how the whole JSON looks like. Thanks.

If I understand you correctly:

return items.filter(i => i.json.codes).map(i => {
  const match = i.json.codes.match(/([^, ]+)/g);

  if (!match) return null;

  const segments = match.toString().split(',').map(x => x.trim());

  let found;

  for (s of segments) {
    const match = s.match(/.*A.*/);
    if (match) {
      found = match[0];
      break;
    }
  }

  if (!found) return null;

  return { json: { codes: found } };
});
1 Like

Yes, thank you very much, worked flawless :slight_smile:.