How to convert a comma-delim list to an array?

Hi, this is my first post here. Apologies if this is super-basic but I’m struggling with converting a simple comma-delimited list to an array object. What I have now:

1646695954382x973842455138163000,1646695991193x160252698853282620

What I need instead:

“Priority SDGs”: [
“1646695607984x403065198969848100”,
“1646695549641x245349602686572900”
]

What I’ve tried

Suggestions from this thread (although that appears to be operating on JSON to start).

This javascript code node:

I’m a bit out of my technical depth with this. Can anyone point me in the right direction of how I can massage that comma-delim list into an array object I can pass along?

It looks like your topic is missing some important information. Could you provide the following if applicable.

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

Hi, so I tried a different approach here using visual nodes instead of code and I feel like I’m close but still struggling. Here’s a loom that shows what I’m doing:

And here’s my canvas output:

  • n8n version: 1.33.1
  • Database (default: SQLite): SQLite
  • n8n EXECUTIONS_PROCESS setting (default: own, main): default
  • Running n8n via (Docker, npm, n8n cloud, desktop app): Docker self-hosted on GCP
  • Operating system: Ubuntu for server, MacOS for my machine

You can break up a string into an array using the split() operation. You can use it in an expression like this. (I also added an example of then converting the array into items in case you need that).

Let me know if this solves the issue, or if you also need help with your second approach?

2 Likes

@bartv thx so much for pointing me to the split() function. That got it into the expected format that the HTTPpost wants but I need to do essentially a substitution from a lookup table to replace those string names with their database ID’s. I know how i would do this in Google Sheets via vLookup. Do you know of any equivalent function or node type in n8n that can accomplish this substitution keyed off the name as the join field?

This shows you where I’m at with things and this is the last piece to work out to put this to bed:

I’ve been going through the Level 2 tutorial on a merging scenario but I’m missing something in how to match my fields against each other. Something is off with my data types and I don’t grasp the dot.notation syntax and javascript array stuff well enough yet to see what the issue is.

Thanks for any tips you may have at this point.
sean

Hi, just thought I’d close the loop on this and show the solution I came up with to finally finish this. Unfortunately I never figured out the vLookup equivalent in n8n (I’m sure there’s a clever way to accomplish the same thing either in code or with a chain of nodes but it was beyond me so I just did this hack):

  1. In Google Sheets I have the lookup table on a separate sheet:

I made a new column in the sheet with the people data called SDGS BY ID, copied the named SDGs and then did a find/replace one-by-one for all 17 SDGs on that column in the Gsheet. That yielded what I needed in terms of a field that had a comma-delim list of the database ID’s for each SDG for that person. I did have to do a find/replace to eliminate the space after the comma to get it to work.

  1. Then in n8n I created a Set node and used this expression to strip the offending \n\n chars:
{{ $json['SDGS BY ID '].replace(/[\r\n]/gm, '').trim() }}

and then added @bartv’s Set node above after it to convert that into an array:

{{$json['SDGSstripped'].split(',')}}

And that worked!
This is what the final pipeline looks like:

Not a very elegant/resilient solution but I only needed a one-time import vs. an ongoing sync operation so this was adequate to get what I needed. If anyone knows how to move that lookup table logic into n8n itself eliminating the find/replace tapdance to get the database ID’s, I would love to see that in action. Hope this helps someone.

2 Likes

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