Code Node: Split string into array of strings

This should be a very simple issue, but I’ve been racking my brain on this for much too long. Below is the latest iteration of the function. I either get an error that the return type is not correct or I get “[object Object]” in my json.

Simple, input a string of text. Separate that text into an array of strings. Return the array.

Current implementation

const text = String($input.first());
const lines = text.split("\n");
return [{json: {output: JSON.parse(JSON.stringify(lines))} }];

Input

[
    { 
        "text": "Line #1\nLine #2\nLine #3"
    }
]

Output returned by the last node

[
  {
    "output": [
      "[object Object]"
    ]
  }
]

Expected Output

[
    {
        output: [
            "Line #1",
            "Line #2",
            "Line #3"
        ]
    }
]

Information on your n8n setup

  • 1.91.3:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system: Windows 11

do console.log(text), make sure you’re correctly capturing your input, oh and console.log will show up in your browsers console so hit F12 → console → run code look for output

This should do it:

return [{
    json: {
        output: $input.first().json.text.split("\n")
    }
}];

You don’t need to stringify() and parse() afterwards.

1 Like

This worked perfectly. I see I wasn’t drilling down far enough with the input parameter.

Thanks

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