Limit Items in JSON Value

I need some help modifying json data as part of a larger csv edit workflow. In this step, I’m trying to split (or at the very least limit) the URLs in the ‘PHOTO URL’ field at 5 and push the remaining into an overflow field.

I tried using a Set node with an expression using .slice() but it ended up just slicing the value itself, not treating each item inside the value as an item to slice. (not even sure if i’m wording this right because js is not my forté).

In my current workflow, the JSON data looks like this:

[
    {
        "NAME": 31057,
        "PHOTO URL": "https://url.one; https://url.two; https://url.three; https://url.four; https://url.five; https://url.six; https://url.seven; https://url.eight; https://url.nine; https://url.ten;"
    },
 {
        "NAME": 32000,
        "PHOTO URL": "https://url.one; https://url.two; https://url.three; https://url.four; https://url.five; https://url.six; https://url.seven; https://url.eight; https://url.nine; https://url.ten;"
    },
]

The goal is for the data to look like this:

[
    {
        "NAME": 31057,
        "PHOTO URL": "https://url.one; https://url.two; https://url.three; https://url.four; https://url.five",
        "photo-overflow": "https://url.six; https://url.seven; https://url.eight; https://url.nine; https://url.ten;"
    },
 {
        "NAME": 32000,
        "PHOTO URL": "https://url.one; https://url.two; https://url.three; https://url.four; https://url.five",
        "photo-overflow": "https://url.six; https://url.seven; https://url.eight; https://url.nine; https://url.ten;"
    },
]

Any help is appreciated!



Edited with the solution i implemented with the help of @BramKn

What my nodes ended up looking like was below:

I used a Set node first with the expression:

{{json["PHOTO URL"].split("; ").slice(0,5).join("; ")}}

followed by a function item node

const overflowUrl = $node["original-csv"].json["PHOTO URL"].split("; ").slice(5).join("; ")}};

item.overflowPhoto = overflowUrl;

return item;
  • n8n version: 0.169.0
  • Running n8n via [Docker, npm, n8n.cloud, desktop app]: npm

Hi @patrick_k

Welcome to the community.

One way is to split it into an array and then choose which array Items you would like to get.
first we split: inputfield.split(";")
Result is an array of the Urls. You need to slice this array to get the first 5 items of the array.
then we slice: array.slice(0,4)
and again for the overflow: array.slice(5)
and then if you want the string again like you had you will have to join the values you sliced.
Join them both: slice.join(";")

all together now:
photo URL field: inputfield.split(";").slice(0,4).join(";")
Overflow field: inputfield.split(";").slice(5).join(";")

Hope this is clear. (might have made a typo somewhere, haven’t tested it.

3 Likes

Hi @BramKn, thanks very much for the welcome and the excellent solution!

What my nodes ended up looking like was below:

I used a Set node first with the expression:

{{json["PHOTO URL"].split("; ").slice(0,5).join("; ")}}

followed by a function item node

const overflowUrl = $node["original-csv"].json["PHOTO URL"].split("; ").slice(5).join("; ")}};

item.overflowPhoto = overflowUrl;

return item;

One thing that came out weird and I’m not sure why was I had to slice(0,5) to get 5 items, and then i had to slice(5) to get the next corresponding items. Slicing(0,4) was only giving 4 urls (1-4), and slicing(5) after that was missing the fifth url (6-10).

1 Like

Hi @patrick_k
You are very welcome.

The unexpected behaviour doesn’t sound that weird to me. With those kinda things it’s always just a case of playing with it to get the correct set. Glad you figured it out, hope it didn’t take too long for you to fix it. :+1:

1 Like

Hi @patrick_k and @BramKn , I hope you don’t mind me adding a sample workflow of this challenge. I’m interested in learning all about data-transformation, and for this, I’m looking at all challenges and I’m working out sample workflows in a (in the meantime big) workflow …

I’ve also added a small twist by doing both splits in a single Set Node …

2 Likes

It completely slipped my mind that I could combine both transformations into a single set node by just adding another set! Thanks @dickhoning for chiming in :smile:

1 Like

Hi @patrick_k great to hear that my little contribution helped you figure it out, and thanks for letting me know!

1 Like