# Transform Array to String

**URL:** <https://community.n8n.io/t/transform-array-to-string/12857>\
**Category:** Questions\
**Tags:** data-transformation, transform-array\
**Created:** [April 4, 2022, 9:15pm UTC](https://community.n8n.io/t/transform-array-to-string/12857 "2022-04-04T21:15:24Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![SELLIFIC](https://avatars.discourse-cdn.com/v4/letter/s/54ee81/32.png) [@SELLIFIC](https://community.n8n.io/u/SELLIFIC)\
**Post date:** [April 4, 2022, 9:15pm UTC](https://community.n8n.io/t/transform-array-to-string/12857/1 "2022-04-04T21:15:24Z")

</div>

Hi, how can i convert data from array to string.  
I need to send an array of objects in HTTP Request as query parameters which must have this format:

[https://example.com/jsonAPI.php?pozycje[0][cena\_netto]=8.99&pozycje[0][ilosc]=1&](https://example.com/jsonAPI.php?pozycje%5B0%5D%5Bcena_netto%5D=8.99&pozycje%5B0%5D%5Bilosc%5D=1&)…

```auto
`pozycje[${index}][cena_netto]=${element.cena_netto}&pozycje[${index}][ilosc]=${element.ilosc}&pozycje[${index}][id_produktu]=${element.id_produktu}&pozycje[${index}][jednostka]=${element.jednostka}`

```

How can I transfer this code to n8n or make something that works the same as this code below?

The code from my javascript API integration application looks like this:

```auto
const productsArray2 = [
  {
    product_id: '69910511',
    name: 'Product name 8441',
    sku: '8441',
    ean: '1104517008441',
    quantity: 1,
    id: '40',
    unit: 'pcs.',
    price_netto: '8.9900',
  },
  {
    product_id: '69743959',
    name: 'Product name 4708',
    sku: '4708',
    ean: '1104517044708',
    quantity: 1,
    id: '23',
    unit: 'pcs.',
    price_netto: '5.2900',
  },
];

const transformToURL = function () {
  const transformedToURL = productsArray2
    .map(function (element, index) {
      return `pozycje[${index}][cena_netto]=${element.price_netto}&pozycje[${index}][ilosc]=${element.quantity}&pozycje[${index}][id_produktu]=${element.id}&pozycje[${index}][jednostka]=${element.unit}`;
    })
    .join('&');
  // return transformedToURL;
  console.log(transformedToURL);
};

transformToURL();

```

console log result:

> pozycje[0][cena\_netto]=8.9900&pozycje[0][ilosc]=1&pozycje[0][id\_produktu]=40&pozycje[0][jednostka]=pcs.&pozycje[1][cena\_netto]=5.2900&pozycje[1][ilosc]=1&pozycje[1][id\_produktu]=23&pozycje[1][jednostka]=pcs.

---

<div class="post-metadata">

**Author:** ![dickhoning](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/dickhoning/32/10415_2.png) [@dickhoning](https://community.n8n.io/u/dickhoning)\
**Post date:** [April 5, 2022, 8:55am UTC](https://community.n8n.io/t/transform-array-to-string/12857/2 "2022-04-05T08:55:01Z")

</div>

Hi @SELLIFIC hope this example helps you ge started:

You can construct the code via an expression in the HTTP request url …

 ![Screenshot 2022-04-05 at 10.50.14](https://europe1.discourse-cdn.com/n8n/original/2X/a/a57b164f72a569599a50e93ea2a7e7aa9a28cbd7.png)

P.s.: in order to create the sample data, I first had to convert your javascript array into JSON. I did this in VS Code:

```auto
let myArr = ([
    {
      product_id: '69910511',
      name: 'Product name 8441',
      sku: '8441',
      ean: '1104517008441',
      quantity: 1,
      id: '40',
      unit: 'pcs.',
      price_netto: '8.9900',
    },
    {
      product_id: '69743959',
      name: 'Product name 4708',
      sku: '4708',
      ean: '1104517044708',
      quantity: 1,
      id: '23',
      unit: 'pcs.',
      price_netto: '5.2900',
    },
  ])

  console.log(JSON.stringify(myArr))

```

---

<div class="post-metadata">

**Author:** ![SELLIFIC](https://avatars.discourse-cdn.com/v4/letter/s/54ee81/32.png) [@SELLIFIC](https://community.n8n.io/u/SELLIFIC)\
**Post date:** [April 5, 2022, 9:38am UTC](https://community.n8n.io/t/transform-array-to-string/12857/3 "2022-04-05T09:38:39Z")

</div>

@dickhoning How can I insert index of current object in expression like here ( ${index} ):

```auto
`pozycje[${index}][cena_netto]=${element.cena_netto}&`

```

And at the end join everything using join `.join('&')`

---

<div class="post-metadata">

**Author:** ![dickhoning](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/dickhoning/32/10415_2.png) [@dickhoning](https://community.n8n.io/u/dickhoning)\
**Post date:** [April 5, 2022, 3:22pm UTC](https://community.n8n.io/t/transform-array-to-string/12857/4 "2022-04-05T15:22:42Z")

</div>

@SELLIFIC it’s only now that I get what you’re looking for. Sorry! You’re probably looking for an iteration / for of JavaScript to build this string and return it as a value in a json object, which you can then use in an expression in the http node. I’m currently not fully up to speed with iteration in n8n, and hope that another member will jump in to help you. I know @MutedJam is very handy with this 🕶

---

<div class="post-metadata">

**Author:** ![SELLIFIC](https://avatars.discourse-cdn.com/v4/letter/s/54ee81/32.png) [@SELLIFIC](https://community.n8n.io/u/SELLIFIC)\
**Post date:** [April 5, 2022, 8:29pm UTC](https://community.n8n.io/t/transform-array-to-string/12857/5 "2022-04-05T20:29:24Z")

</div>

I found a variable that is responsible for the index

" `$position`: the index of an item in a list of items "

example:

```auto
pozycje[{{$position}}][cena_netto]={{$json["cena_netto"]}}&

```

---

<div class="post-metadata">

**Author:** ![dickhoning](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/dickhoning/32/10415_2.png) [@dickhoning](https://community.n8n.io/u/dickhoning)\
**Post date:** [April 5, 2022, 8:51pm UTC](https://community.n8n.io/t/transform-array-to-string/12857/6 "2022-04-05T20:51:05Z")

</div>

Hi @SELLIFIC maybe this might help you …

---

<div class="post-metadata">

**Author:** ![MutedJam](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/mutedjam/32/70851_2.png) [@MutedJam](https://community.n8n.io/u/MutedJam)\
**Post date:** [April 6, 2022, 7:18am UTC](https://community.n8n.io/t/transform-array-to-string/12857/7 "2022-04-06T07:18:55Z")

</div>

Hey all, thanks for tagging me 😃

I’d say if you have working code keep it @SELLIFIC. Here’s an example sticking pretty much to your original code, just split up across two nodes (one [Function node](https://docs.n8n.io/nodes/n8n-nodes-base.function/) mocking the incoming data and a [Function Item](https://docs.n8n.io/nodes/n8n-nodes-base.functionItem/) node for the transformation).

You’d end up with a request like so:

 ![image](https://europe1.discourse-cdn.com/n8n/original/2X/f/f6e22d0e1af355c3b5080953ef4ca36f6c212efb.png)

---

<div class="post-metadata">

**Author:** ![dickhoning](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/dickhoning/32/10415_2.png) [@dickhoning](https://community.n8n.io/u/dickhoning)\
**Post date:** [April 6, 2022, 7:55am UTC](https://community.n8n.io/t/transform-array-to-string/12857/8 "2022-04-06T07:55:08Z")

</div>

Thanks @MutedJam now also part of my data transformation collection 😎 Just one question; your mock data does not work. Is this because I’m using the Desktop App (1.3) which is n8n version 0.165.1?

 ![Screenshot 2022-04-06 at 09.53.37](https://europe1.discourse-cdn.com/n8n/original/2X/2/21ae584e80176a8399de5b59b46e4d68956d97e3.png)

---

<div class="post-metadata">

**Author:** ![MutedJam](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/mutedjam/32/70851_2.png) [@MutedJam](https://community.n8n.io/u/MutedJam)\
**Post date:** [April 6, 2022, 7:58am UTC](https://community.n8n.io/t/transform-array-to-string/12857/9 "2022-04-06T07:58:28Z")

</div>

Good point @dickhoning, that’s because n8n before 0.166.0 would still expect the “proper” [n8n data structure](https://docs.n8n.io/getting-started/key-concepts/#data-structure), later versions are more lenient. Here’s the workflow version working with older versions as well:

The credit should go @SELLIFIC though, I merely copied the existing code 😃

---

<div class="post-metadata">

**Author:** ![SELLIFIC](https://avatars.discourse-cdn.com/v4/letter/s/54ee81/32.png) [@SELLIFIC](https://community.n8n.io/u/SELLIFIC)\
**Post date:** [April 6, 2022, 1:07pm UTC](https://community.n8n.io/t/transform-array-to-string/12857/10 "2022-04-06T13:07:31Z")

</div>

Thanks @MutedJam for the solution. Node “Transform Data” works great but only with node “Mock Data”. What “Merge” returns I have to convert to URL.  
In my workflow Merge Node returns:

```auto
[
  {
   "product_id": "69910511",
   "name": "Product name 8441",
   "sku": "8441",
   "ean": "1104517008441",
   "quantity": 1,
   "id": "40",
   "unit": "pcs.",
   "price_netto": "8.9900"
   },
   {
   "product_id": "69743959",
   "name": "Product name 4708",
   "sku": "4708",
   "ean": "1104517044708",
   "quantity": 1,
   "id": "23",
   "unit": "pcs.",
   "price_netto": "5.2900"
   }
]

```

Your “Mock Data” returns:

```auto
[
  {
  "products": [
    {
    "product_id": "69910511",
    "name": "Product name 8441",
    "sku": "8441",
    "ean": "1104517008441",
    "quantity": 1,
    "id": "40",
    "unit": "pcs.",
    "price_netto": "8.9900"
    },
    {
    "product_id": "69743959",
    "name": "Product name 4708",
    "sku": "4708",
    "ean": "1104517044708",
    "quantity": 1,
    "id": "23",
    "unit": "pcs.",
    "price_netto": "5.2900"
    }
   ]
  }
]

```

So now I have a problem to either transform what the merge node returns or change the “Transform Data” to work with my “Merge Node”

 ![Zrzut ekranu 2022-04-6 o 14.40.27](https://europe1.discourse-cdn.com/n8n/original/2X/b/bbbf7bda13298f876dfe231996c60cc5902e7cdd.png)  
 ![Zrzut ekranu 2022-04-6 o 14.40.37](https://europe1.discourse-cdn.com/n8n/original/2X/1/12fb3357eb39afdfb15350ed6dab6fb6e44f97df.png)

---

<div class="post-metadata">

**Author:** ![MutedJam](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/mutedjam/32/70851_2.png) [@MutedJam](https://community.n8n.io/u/MutedJam)\
**Post date:** [April 6, 2022, 1:25pm UTC](https://community.n8n.io/t/transform-array-to-string/12857/11 "2022-04-06T13:25:44Z")

</div>

Ah sorry, I wasn’t aware of your exact data structure. Seeing you already have separate n8n items, you’d need to swap my Function Item node for a Function node which has access to all n8n items at once. This is how it’d look in a workflow:

---

<div class="post-metadata">

**Author:** ![SELLIFIC](https://avatars.discourse-cdn.com/v4/letter/s/54ee81/32.png) [@SELLIFIC](https://community.n8n.io/u/SELLIFIC)\
**Post date:** [April 6, 2022, 2:07pm UTC](https://community.n8n.io/t/transform-array-to-string/12857/12 "2022-04-06T14:07:04Z")

</div>

Thanks @MutedJam, now everything works awesome.

---

<div class="post-metadata">

**Author:** ![dickhoning](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/dickhoning/32/10415_2.png) [@dickhoning](https://community.n8n.io/u/dickhoning)\
**Post date:** [April 6, 2022, 6:16pm UTC](https://community.n8n.io/t/transform-array-to-string/12857/13 "2022-04-06T18:16:11Z")

</div>

@MutedJam can you please tell me where I can find more info on the changes (more lenient) in n8n’s data structure? I’ve looked at your examples, and it looks like this simplifies things quite a bit.

---

<div class="post-metadata">

**Author:** ![MutedJam](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/mutedjam/32/70851_2.png) [@MutedJam](https://community.n8n.io/u/MutedJam)\
**Post date:** [April 7, 2022, 6:18am UTC](https://community.n8n.io/t/transform-array-to-string/12857/14 "2022-04-07T06:18:48Z")

</div>

Sure (and it definitely makes things easier!). There was no separate announcement, but this is mentioned in [our changelog](https://docs.n8n.io/reference/release-notes.html#enhanced-nodes-%F0%9F%9A%80):

> [Function:](https://docs.n8n.io/nodes/n8n-nodes-base.function/) Added support for items without a JSON key.

The Function node’s documentation also covers this:

> **Skipping the ‘json’ key and array syntax**  
> From 0.166.0 onwards, n8n automatically adds the json key if it is missing. It also automatically wraps your items in an array () if needed.

So with the next desktop app update this should also work on n8n desktop 🙂

---

<div class="post-metadata">

**Author:** ![dickhoning](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/dickhoning/32/10415_2.png) [@dickhoning](https://community.n8n.io/u/dickhoning)\
**Post date:** [April 7, 2022, 6:31am UTC](https://community.n8n.io/t/transform-array-to-string/12857/15 "2022-04-07T06:31:42Z")

</div>

@MutedJam thanks for the info! And uh … well hidden 😉

---

<div class="post-metadata">

**Author:** ![Gabriele\_Bracciali](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/gabriele_bracciali/32/31293_2.png) [@Gabriele\_Bracciali](https://community.n8n.io/u/Gabriele_Bracciali)\
**Post date:** [January 11, 2023, 3:26pm UTC](https://community.n8n.io/t/transform-array-to-string/12857/16 "2023-01-11T15:26:54Z")

</div>

Hello everyone and @MutedJam, do You know how to use this script with the new Code node v 0.206.1  
Thanks!

---

<div class="post-metadata">

**Author:** ![Jon](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/jon/32/40174_2.png) [@Jon](https://community.n8n.io/u/Jon)\
**Post date:** [January 11, 2023, 3:33pm UTC](https://community.n8n.io/t/transform-array-to-string/12857/17 "2023-01-11T15:33:47Z")

</div>

Hey @Gabriele_Bracciali,

Here is the same example updated for the Code node, The only change is `items` is no longer an option to get the inputs items for the code node you would use `$input.all()`

---

<div class="post-metadata">

**Author:** ![Gabriele\_Bracciali](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/gabriele_bracciali/32/31293_2.png) [@Gabriele\_Bracciali](https://community.n8n.io/u/Gabriele_Bracciali)\
**Post date:** [January 11, 2023, 3:37pm UTC](https://community.n8n.io/t/transform-array-to-string/12857/18 "2023-01-11T15:37:05Z")

</div>

Okay thanks @Jon , I need only to convert an array like this:

 ![Screenshot 2023-01-11 at 16.36.25](https://europe1.discourse-cdn.com/n8n/original/3X/6/5/6535812d396e7daf91ded04158693986a8690ed5.png)  
in string.

I was looking for this code:

```auto
return $input.all().json.map(item => {
  return {
    json: item
  }
});

```

Could work?

---

<div class="post-metadata">

**Author:** ![Jon](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/jon/32/40174_2.png) [@Jon](https://community.n8n.io/u/Jon)\
**Post date:** [January 11, 2023, 3:50pm UTC](https://community.n8n.io/t/transform-array-to-string/12857/19 "2023-01-11T15:50:31Z")

</div>

There would be a bit more to it than that, What are you expecting the output to look like?

---

<div class="post-metadata">

**Author:** ![Gabriele\_Bracciali](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/gabriele_bracciali/32/31293_2.png) [@Gabriele\_Bracciali](https://community.n8n.io/u/Gabriele_Bracciali)\
**Post date:** [January 11, 2023, 3:51pm UTC](https://community.n8n.io/t/transform-array-to-string/12857/20 "2023-01-11T15:51:42Z")

</div>

My expectation is:  
10,9

So take only campaignId value of each item

[Next page](https://community.n8n.io/t/transform-array-to-string/12857.md?page=2)
