Help splitting a response by commas

So… apologies in advance for the simplistic question. I’ve searched the forums multiple times but because I am not a developer and have VERY limited understanding of javascript (read: no understanding of javascript), I have a hard time understanding what the proper method for this is.

I am getting some data from a google firestore database to run multiple http requests off of that data.

The problem I am having is the data comes back as a comma separated string like this:
“Child Variations”:
“B0849HLLV7,B0849HDJWK,B0849S7DD9,B0874LMLZ9,B0849P8G12,B0874L4RQK, etc”,

I’d like to split that string of data into individual chunks like : “B0849HLLV7”, “B0849HDJWK”, etc - and use those as my parameter in my http request.

I’ll also be running many iterations of this as I’ll have many records to pull, each with their own long list of these child variations.

I have a feeling this is a simple javascript operation…but I am helpless. Any ideas?

Thanks!

Welcome to the community @galaxy2jake

The example below does that. The first node mocks the HTTP request that responds with the string. The second function split the string into individual properties.

Let me know if you have further questions.

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {\n      data: \"B0849HLLV7,B0849HDJWK,B0849S7DD9,B0874LMLZ9,B0849P8G12,B0874L4RQK\"\n    }\n  },\n    {\n    json: {\n      data: \"1,2,3,B0874LMLZ9,B0849P8G12,B0874L4RQK\"\n    }\n  }\n]"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        500,
        300
      ],
      "notesInFlow": true,
      "notes": "Mockup data"
    },
    {
      "parameters": {
        "functionCode": "const results = [];\n\nfor (const item of items) {\n  const element = {};\n  for (const [index, key] of item.json.data.split(',').entries()) {\n    element[`chunk_${index}`] = key;\n  }\n  results.push({ json: element })\n}\nreturn results;"
      },
      "name": "Function1",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        740,
        300
      ],
      "notesInFlow": true,
      "notes": "Split Ddata"
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function": {
      "main": [
        [
          {
            "node": "Function1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
1 Like

Wow! Thank you for your quick response!

I see how your example works, which is great. But I am getting an error when trying to apply it to my data:
ERROR: Cannot read property ‘split’ of undefined

TypeError: Cannot read property 'split' of undefined
    at /usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes:5:45
    at Object.<anonymous> (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes:10:17)
    at NodeVM.run (/usr/local/lib/node_modules/n8n/node_modules/vm2/lib/main.js:1167:29)
    at Object.execute (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Function.node.js:65:31)
    at Workflow.runNode (/usr/local/lib/node_modules/n8n/node_modules/n8n-workflow/dist/src/Workflow.js:492:37)
    at /usr/local/lib/node_modules/n8n/node_modules/n8n-core/dist/src/WorkflowExecute.js:416:62

That is because in my example I assumed that the data came in a property named data. Can you share a screenshot of how your data looks.

1 Like

Ah yes, I see that. I tried swapping out “data” in the second function with “Child Variations” but got an error saying unexpected string.

Try it like this:

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {\n      \"Child Variations\": \"B0849HLLV7,B0849HDJWK,B0849S7DD9,B0874LMLZ9,B0849P8G12,B0874L4RQK\"\n    }\n  },\n    {\n    json: {\n      \"Child Variations\": \"1,2,3,B0874LMLZ9,B0849P8G12,B0874L4RQK\"\n    }\n  }\n]"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        480,
        300
      ],
      "notesInFlow": true,
      "notes": "Mockup data"
    },
    {
      "parameters": {
        "functionCode": "const results = [];\n\nfor (const item of items) {\n  const element = {};\n  for (const [index, key] of item.json['Child Variations'].split(',').entries()) {\n    element[`chunk_${index}`] = key;\n  }\n  results.push({ json: element })\n}\nreturn results;"
      },
      "name": "Function1",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        730,
        300
      ],
      "notesInFlow": true,
      "notes": "Split Ddata"
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function": {
      "main": [
        [
          {
            "node": "Function1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
1 Like

Ah yes, that works! Wow, thanks again for your prompt help! I appreciate the hand holding!

1 Like

@RicardoE105
So now that they are split - which is exactly what I need, how can I get the http request to run for each split item?
In the expression, I can select an individual “chunk” but I can’t select all fo them.

How the http request needs to send the data? I’m guessing in the body. But, does it need an specific structure or you need to send everything as a string?

Actually, for these apis, they just needs them as a query parameter in the url like this: &asin=XXXXXXXXXX

When referencing them individually. Use the query parameters in that HTTP node. When you add a query in the node, the UI it’s going to show you the key and value. Add as many keys as you need and reference the corresponding value (chunk). The HTTP node will automatically be called for as many inputs it gets.

So the issue I have is that the number of items is not tied to the chunks we’ve just created but instead to the database records.

I’m terrible at explaining things but let me attempt to make it clear.

Database will have X number of records. Each record will have what we call a Parent ASIN.

Each Parent ASIN will have any number of Child ASINs. The child asins are what we just split up which is what I needed, because they still need to be tied to the Parent ASIN.

But when I use the expression to use a Child ASIN as the value for the API, it only runs for as many Parent ASINs as I have.

What I need it to do is continue to run for each Child ASIN of each Parent ASIN.

So Parent ASIN 1 may have 100 Child ASINs for example. Parent ASIN 2 may have 50 child ASINs. I need the node to call the API a total of 150 times, but keep the 100 responses tied to Parent ASIN 1, and the 50 responses tied to Parent ASIN 2.

Does that make sense?

Ohh I think I get it. The example below should do it:

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {\n      \"ASIN\": '123',\n      \"Child Variations\": \"B0849HLLV7,B0849HDJWK,B0849S7DD9,B0874LMLZ9,B0849P8G12,B0874L4RQK\"\n    }\n  },\n    {\n    json: {\n      \"ASIN\": '456',\n      \"Child Variations\": \"1,2,3,B0874LMLZ9,B0849P8G12,B0874L4RQK\"\n    }\n  }\n]"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        480,
        300
      ],
      "notesInFlow": true,
      "notes": "Mockup data"
    },
    {
      "parameters": {
        "functionCode": "const results = [];\n\nfor (const item of items) {\n  for (const [index, key] of item.json['Child Variations'].split(',').entries()) {\n    const element = {};\n    Object.assign(element, item.json)\n    element[`Child Variation`] = key;\n    delete element['Child Variations']\n    results.push({ json: element })\n  }\n}\nreturn results;"
      },
      "name": "Function1",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        730,
        300
      ],
      "notesInFlow": true,
      "notes": "Split Ddata"
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function": {
      "main": [
        [
          {
            "node": "Function1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
1 Like

Awesome - thank you!!!

Is there no built-in node to split simple comma separated field to multiple items? Referencing individual chunks makes little to no sense to be honest as it could be a large list of chunks?

Example:
[
{
“chunk_0”: “277070”,
“chunk_1”: “279968”,
“chunk_2”: “384964”,
“chunk_3”: “386320”,
“chunk_4”: “386322”,
“chunk_5”: “386324”,
“chunk_6”: “386326”,
“chunk_7”: “386328”,
“chunk_8”: “386329”,
“chunk_9”: “419073”,
“chunk_10”: “656523”,
“chunk_11”: “663860”,
“chunk_12”: “663861”,
“chunk_13”: “663862”,
“chunk_14”: “663863”,
“chunk_15”: “663865”,
“chunk_16”: “663867”,
“chunk_17”: “751197”,
“chunk_18”: “751202”,
“chunk_19”: “751209”,
“chunk_20”: “752630”,
“chunk_21”: “753342”,
“chunk_22”: “756037”,
“chunk_23”: “812236”,
“chunk_24”: “812244”,
“chunk_25”: “812252”,
“chunk_26”: “812295”,
“chunk_27”: “223611”,
“chunk_28”: “423716”,
“chunk_29”: “423717”,
“chunk_30”: “423718”,
“chunk_31”: “423719”,
“chunk_32”: “424137”,
“chunk_33”: “424138”,
“chunk_34”: “424139”,
“chunk_35”: “424140”
}
]

Which is generated with the above code example from the single comma separated field:

“list”:
“277070,279968,384964,386320,386322,386324,386326,386328,386329,419073,656523,663860,663861,663862,663863,663865,663867,751197,751202,751209,752630,753342,756037,812236,812244,812252,812295,223611,423716,423717,423718,423719,424137,424138,424139,424140”

Whats the trick I am missing here to loop through all the above values to http node as variable?

Hi @lazmo88, seeing you have an object here rather than a string I think you’d need slightly different code to handle this custom transformation.

With that said, it’d be great if you could avoid re-opening old that are already marked as solved. Perhaps you can open a new topic and share the exact JSON data structure you currently have? Thank you so much!

1 Like

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