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!!!