Parameter in expression

Hi,
I’m integrating Facebook with my marketing automation solution, and when a lead is sent from Facebook, I need to check whether it already exists. The result of this check is the following, in case the lead already exists:

[
{
“total”: “1”,
“contacts”: {
“3720”: {
“isPublished”: true,
“dateAdded”: “2021-07-21T12:37:10+00:00”,
“dateModified”: “2021-07-21T18:01:37+00:00”,
“createdBy”: 3,
“id”: 3720,
“points”: 0,
“color”: null,
“fields”: {
“core”: {
“points”: {
etc.

Further along in my flow, I need to reference the values in [contacts][3720][fields][core]. The problem is that 3720 is the id of the lead in my marketing tool, as a result of the GET request, and thus is different for each lead.
How can I build an expression that dynamically updates the proper id value so that the attributes in the json can be accessed? Something like {{$node[“Get MT”].json[“contacts”][“id parameter”][“fields”]["core][“situacao”] is what I need.
Thanks a lot,
Christophe

Hey @cdesseaux,

I’ve created a small workflow that demonstrates this. Assuming that the actually ID is the first key in the Contact object, the expression used the Set node will now get the value dynamically.

{
  "nodes": [
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "string": [
            {
              "name": "id param",
              "value": "={{ $node[\"Function\"].json[\"contacts\"][Object.keys($node[\"Function\"].json[\"contacts\"])[0]][\"points\"] }}"
            }
          ]
        },
        "options": {}
      },
      "name": "Set",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        650,
        450
      ]
    },
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {\n\"total\": 1,\n\"contacts\": {\n\"3720\": {\n\"isPublished\": true,\n\"dateAdded\": \"2021-07-21T12:37:10+00:00\",\n\"dateModified\": \"2021-07-21T18:01:37+00:00\",\n\"createdBy\": 3,\n\"id\": 3720,\n\"points\": 0,\n}\n}\n  }\n  }\n]"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        450,
        450
      ]
    }
  ],
  "connections": {
    "Function": {
      "main": [
        [
          {
            "node": "Set",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Great! That works splendidly. Thanks, @harshil1712 !

1 Like