Generate an Array

Hi,

I want to generate an array having the list of phone numbers. I want to pass that array to call an API. It should look like

[ '9999999999', '8888888888']

Currently the output of my node is this.



I tried using Function Item Node and pushing it into array, but Its generating different arrays for each phone number.

Check the example below:

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {\n      Name: 'test',\n      Phone: 123456\n    }\n  },\n  {\n    json: {\n      Name: 'test2',\n      Phone: 1234562\n    }\n  },\n]"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        540,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "const phones = []\n\n\nfor (const item of items) {\n  phones.push(item.json.Phone)\n}\n\nreturn [\n  {\n    json: {\n      phones,\n    }\n  }\n]\n\n\n"
      },
      "name": "Function1",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        770,
        300
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function": {
      "main": [
        [
          {
            "node": "Function1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
1 Like

More information here:

Thanks a lot @RicardoE105 @ivov :smiley:

Also @RicardoE105 just a small question. How can we remove the [Array:] keyword and just display the array.
Im using the following to get the array from previous node.

Expression
image

Result
image

Expected should be [999999999, 888888888]

Got a temp solution by adding .toString() to the expression.

{{$node["Function4"].json["Phones"].toString()}}

@RicardoE105 correct me if I’m wrong but the “Array:” is simply how we’re visualizing the array in n8n front end (i.e. basically linting it). Same thing happens if you add in a JSON object (Object:). So far, any api or node that I’ve used with Objects/ Arrays as parameter inputs have worked.

Do let me know @Asit_Joshi if you’re getting a different experience when using it.

That’s my understanding as well. @Asit_Joshi can you confirm that the data type of the chartIds parameter is expected to be an array? Asking this because yesterday I came across an API that expected a JSON object to be sent as a string.

More than happy to take a look if you can share the name of the API you are sending the data to :slight_smile:

Yes @tanay,

The chatIds parameter is expecting an Array of strings.

So the ideal request body should be:

chatIds: ["9999999999", "8888888888","8888888888"],

Yes, the [Array: ...] or [Object: ...] does not get sent anywhere. It literally just means this is an actual Array or Object and not just a string.

And if you want to convert it to a string .toString() would be the wrong method to use as the output would look different. You would have to use JSON.stringify($node...).

2 Likes

Thanks @jan,

JSON.stringify() perfectly worked for it and is providing the array in the request.

Thanks everyone :smiley:

2 Likes

Glad to hear that it worked. Have fun!