Split String to Array

Hello.

Can someone help me split this


[
{
"values": "111, 222, 333"
}
] 

into this


[
{
"values": "111"
},
{
"values": "222"
},
{
"values": "333"
}
] 

You could do so via a short JS snippet using String.split().

Example Workflow
{
  "nodes": [
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "values",
              "value": "111, 222, 333"
            }
          ]
        },
        "options": {}
      },
      "name": "Set Example Data",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "let myValues = [];\n\nfor (item of items) {\n  myValues.push(...item.json.values.split(','));\n}\n\nreturn myValues.map(e => {\n  return {\n    json: {\n      values: e.trim()\n    }\n  };\n});"
      },
      "name": "Split String",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    }
  ],
  "connections": {
    "Set Example Data": {
      "main": [
        [
          {
            "node": "Split String",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

image

This example would also work for multiple input items with an array in each. If you ever only have one input item you can simplify it a bit as you wouldn’t need to loop through multiple items.

1 Like

Thank you very much.

It works.

1 Like