Creating an array in an expression

I am trying to connect up to an API and in their documentation I need to create inside an http request an array like this:

    'plainPassword' => array(
        'password' => 'topSecret007',
        'confirm' => 'topSecret007',
    ),

how would i add this to a body parameter ?

So as I know js does not support associative arrays, so you could use js object:

{
  "nodes": [
    {
      "parameters": {
        "functionCode": "return [{\n \"json\": {\n      \"val1\": \"supersecret\",\n      \"val2\": \"secret\"\n }\n}]"
      },
      "name": "Function1",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        -880,
        740
      ]
    },
    {
      "parameters": {
        "requestMethod": "POST",
        "options": {},
        "bodyParametersUi": {
          "parameter": [
            {
              "name": "plainPassword",
              "value": "={{$json}}"
            }
          ]
        },
        "queryParametersUi": {
          "parameter": []
        }
      },
      "name": "HTTP Request1",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        -660,
        740
      ]
    }
  ],
  "connections": {
    "Function1": {
      "main": [
        [
          {
            "node": "HTTP Request1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Or you could make manipulate object to string to have this syntax

1 Like

In case you have to send it as an actual array, then you would have to set an expression like this:
{{ {"plainPassword": [{"password": "topSecret007", "confirm": "topSecret007" }]} }}

Here an example workflow:

{
  "nodes": [
    {
      "parameters": {
        "requestMethod": "POST",
        "jsonParameters": true,
        "options": {},
        "bodyParametersJson": "={{ {\"plainPassword\": [{\"password\": \"topSecret007\", \"confirm\": \"topSecret007\" }]} }}"
      },
      "name": "HTTP Request11",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        840,
        540
      ]
    }
  ],
  "connections": {}
}
1 Like

Thanks both for the advice. I have tried multiple variations however nothing is working for me yet.

From the mautic API this is what I am meant to be doing:

$data = array(
    'username' => 'apitest',
    'firstName' => 'John',
    'lastName' => 'Doe',
    'email' => '[email protected]',
    'plainPassword' => array(
        'password' => 'topSecret007',
        'confirm' => 'topSecret007',
    ),
    'role' => 1,
);

This is what i have tried to do so far

{
  "nodes": [
    {
      "parameters": {
        "authentication": "basicAuth",
        "requestMethod": "POST",
        "url": "https://mydomain.com/api/users/new",
        "options": {},
        "bodyParametersUi": {
          "parameter": [
            {
              "name": "username",
              "value": "test123"
            },
            {
              "name": "firstName",
              "value": "mike"
            },
            {
              "name": "lastName",
              "value": "test"
            },
            {
              "name": "email",
              "value": "[email protected]"
            },
            {
              "name": "role",
              "value": "1"
            },
            {
              "name": "plainPassword",
              "value": "={{ [{\"password\":\"topSecret007\", \"confirm\": \"topSecret007\" }] }}"
            }
          ]
        },
        "headerParametersUi": {
          "parameter": []
        },
        "queryParametersUi": {
          "parameter": []
        }
      },
      "name": "HTTP Request3",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        1060,
        110
      ],
      "credentials": {
        "httpBasicAuth": "AMS"
      }
    }
  ],
  "connections": {}
}

Try it like this: {{ {"password":"topSecret007", "confirm": "topSecret007" } }}

1 Like

Thanks @RicardoE105 - that did the trick! Much appreciated.

Great that it worked. Have fun.

could i get a little bit more help… how would i pass a node value into this ?

{{ {"password":"{{$node["Set"].json["password"]}}", "confirm": "{{$node["Set"].json["password"]}}" } }}
when i view the result i get this

[invalid (Expression is not valid: Unexpected string)]", “confirm”: “mike1234” } }}

You would have to use:

{{ {"password": $node["Set"].json["password"], "confirm": $node["Set"].json["password"] } }}

Honestly this is the fastest forum i have ever worked on. This is the most incredible piece of technology developed changing the way less technical people can work.

Thank you so much for this amazing product and the great support!!

4 Likes

Thanks a lot! That is great to hear! We may not be able to promise you that it will be always that fast but we are definitely always doing our best!

3 Likes