Unable to split array

I have a column with multiple arrays and I’m trying to split out the elements, but they don’t have names, do I need to define each one? I’ve tried multiple things but nothing seems to work. I found another post with similar issue and tried the most recent solution pasted below.

The column with the arrays I’m trying to parse is called csv which you can see in the workflow.

Current workflow returns the following error;

Problem executing workflow

There was a problem executing the workflow:
“Unexpected token u in JSON at position 0 [Line 4]”

I copied the code in the function from another post, so maybe it’s not what I should be doing here?

Specifically what I need solved is that I have an multi-level array that looks something like this;

[
[
5883052,
-1
],
[
5883052,
3899,
6017332,
3898,
6017516,
3899
]
]

Each array means something, which I can define from the source, and each of those elements in the array also has a meaning which I can also define, but maybe I have to assign that definitions when I parse this out?

So the output of the above would be 4 columns. First column would have the first element of the first array. Then 2nd column would have the 2nd element of the first array. 3rd column would have 3 values, it would be each of the 7 digit numbers from the 2nd array, 4th column would be the 2nd value of each pairing. Hopefully that makes sense.

Hi @JasonA, seeing you already have the Split Into Items option enabled I suppose it didn’t help much here.

So, you’d have to manually parse your array items (which are arrays in itself) into suitable n8n items. You wouldn’t need to split them out though seeing they are separate items already.

Here’s an example approach I’ve created based on your data using the Set node. Let me know if you have any questions on this.

If you’re using a recent version of n8n you could set up this node using drag & drop like so:

Recording 2022-10-11 at 08.59.49

1 Like

This has gotten be closer by being able to use the set node, but the issue is still that it’s looking at each element as a different field. What I mean by that is this,… that first array, 0,0 and 0,1. fine no issues separate the two values into that first row. But then in the second array each of those 7 digit numbers is actually a funky way of providing the Unix Epoch time, I’ll actually have to do some additional manipulating after I figure this part out,… but all those values are the same definition within that array, same with the 4 digit numbers in that array they are all the same,… they are actually “price”,… how do I define each pairing so essentially each array should just give me two columns of data, “Date” & “Price”.

You did help my split out the arrays though so that at least for now I can work within a single array to try and define the elements within that array.

This is what I have so far based on your suggestion where I was able to now isolate the 2nd array;

Hm, I am not sure I fully understand. Could you perhaps share the full JSON you currently have (feel free to redact the actual values, I am just interested in the data structure) and the JSON data you would like to see instead?

Here is a snippet of the JSON response I’m getting from the source site,… this is only a snippet as the amount of data is too large to paste here and it wouldn’t let me upload a notepad file here.

[
{
"timestamp": 1665487994803,
"tokensLeft": 294,
"refillIn": 36581,
"refillRate": 5,
"tokenFlowReduction": 0,
"tokensConsumed": 6,
"processingTimeInMs": 11,
"products": [
{
"csv": [
[
5543728,
-1
],
[
6179436,
2595,
6181980,
2593,
6182112,
2589,
6182264,
2585,
6183806,
2582,
6183874,
2580,
6184062,
2576
]
]
}
}
]

Notice there are multiple arrays within the csv. For each array, I want to be able to output the data like the attached sample table (used data from the 2nd array that is pasted above);

Sample Data from JSON

Hi @JasonA, this seems to be invalid JSON which makes it really hard to know whether I am on the right track. This is in addition to the other problems: Can there be multiple products items or just one? Same with csv. I just have no idea how exactly this format works. So, this isn’t exactly about splitting an array but mostly guesswork instead.

Purely based on the snippet you have you have provided and with a bit of guessing as to how the JSON structure continues, I think code like this could do the job:

This will convert the part highlighted below into a field of its own:

If you’d like to have individual items for each of these entries you can combine the above workflow with an Item Lists node:

That worked !! Thank You.

1 Like

Does this no longer work with the code node? Looks like most recent versions code node replaced function item, but I see in the docs looks like maybe impacted this to work using code? below is my modified version of what you provided above.

I’m getting this error
ERROR: item is not defined. Did you mean $input.item.json? [line 12, for item 0]

Note, this exact code works on previous version using function item. And I don’t know how to provide sample data here because this only allows me to upload an image file.