Please help with joining inner array values as a string

Hi!

I currently have this as an output:

[ { 

"Id": 207297,
"Category": "Запчасти и аксессуары",
"Images":    {"Image": [
                                     {"url": "https://zaag.com/f26d.jpg"},
                                     {"url": "https://zaag.com/ba1.jpg"},
                                     {"url":"https://zaag.com/2664.jpg"},
                                     {"url":"https://zaag.com/d5b6.jpg"},
                                     {"url": "https://zaag.com/d1ce.jpg"}
                                        ]
}},

{
"Id": 207296,
"Category": "Запчасти и аксессуары",
"Images":    {"Image": [
                                   {"url":"https://zaag.com/575be.jpg"},
                                   {"url":"https://zaag.com/8bc4a726d.jpg"},
                                   {"url":"https://zaag.com/50cb1.jpg"},
                                   {"url":"https://zapcarsvag.com/aa966e.jpg"}
                                       ] 
}} ]

and I need to connect the picture links with the " | " sign:

[ {

"Id": 207297,
"Category": "Запчасти и аксессуары",
"Images":   "https://zaag.com/f26d.jpg | https://zaag.com/ba1.jpg | https://zaag.com/2664.jpg |  https://zaag.com/d5b6.jpg | https://zaag.com/d1ce.jpg"},

{
"Id": 207296,
"Category": "Запчасти и аксессуары",
"Images":   "https://zaag.com/575be.jpg | https://zaag.com/8bc4a726d.jpg | https://zaag.com/50cb1.jpg | https://zapcarsvag.com/aa966e.jpg"}

]

==============================================

It seems to me that it should be done through Function Item node. But it doesn’t work for me.

return {
  myNewField: item.Images.Image.map(e => e.text).join(' | ')
};

Or set node, somethinng like that:

{{$json["Images"]["Image"].text.join(' | ')}}

Can you tell me how to solve this problem?

Thank you very much!

**This problem originally came from this branch:

Hey Mikhail,
with the Function Item node it could work like this.

Here is an example workflow:

1 Like

Hey @marcus

Thank you so much for answering!

Unfortunately, I have an error:

Can you check the workflow? :

Hey Mikhail,
that is because your xml does not always has an array of images, sometimes its just an url field and sometimes it doesn’t exist. I’ve updated the workflow to work accordingly.

1 Like

@marcus

Just super! Everything works great! :+1: :handshake:
I forgot to ask to add one condition…
Could you add the condition that if there are more than 10 links (url) - use only the first 10:

if(item.Images && item.Images.Image) {
  const image = item.Images.Image;
  if(image.map) {
    item.Images = image.map(img => img.url).join(' | ');
  } else if(image.url) {
    item.Images = image.url;
  }
}

return item;

Hey Mikhail,
this should work.

1 Like

Hey @marcus !

With the last update (0.199.0) FunctionItem node took a very long time to execute - 9-10 min with CPU overload.

Before that (0.197.1), it worked faster (1-2 min) and without overloading CPU.

if(item.Images && item.Images.Image) {
  const image = item.Images.Image;
  if(image.map) {
    item.Images = image.map(img => img.url).slice(0, 10).join(' | ');
  } else if(image.url) {
    item.Images = image.url;
  }
}

return item;

Is there any way to fix it?

Thank you!

Hi @Mikhail,
can you try out the following workflow. I replacted the Function Item node with the new Code node and it’s running much quicker now.

We are currently investigating some performance issues but this should improve your experience for now.

2 Likes

@marcus

Wow!!! Unbelievable! That was really fast - just dozens of times faster! :+1: :muscle:

Thank you so much for your help and a special big thank you for THE speed of response!!!

1 Like