Transform Array to String

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&

`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:

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.

Hi @SELLIFIC hope this example helps you ge started:

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

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:

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))

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

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

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

1 Like

@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 :sunglasses:

1 Like

I found a variable that is responsible for the index

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

example:

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

Hi @SELLIFIC maybe this might help you …

Hey all, thanks for tagging me :smiley:

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 mocking the incoming data and a Function Item node for the transformation).

You’d end up with a request like so:

1 Like

Thanks @MutedJam now also part of my data transformation collection :sunglasses: 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?

1 Like

Good point @dickhoning, that’s because n8n before 0.166.0 would still expect the “proper” n8n 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 :smiley:

1 Like

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:

[
  {
   "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:

[
  {
  "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”


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:

1 Like

Thanks @MutedJam, now everything works awesome.

1 Like

@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.

Sure (and it definitely makes things easier!). There was no separate announcement, but this is mentioned in our changelog:

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 :slight_smile:

1 Like

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

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

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()

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


in string.

I was looking for this code:

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

Could work?

1 Like

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

My expectation is:
10,9

So take only campaignId value of each item