Help with workflow: Switching off of array data

Hey all,
I am making a workflow to take Hubspot contacts, verify their phone type (mobile, landline, voip) with the twilio lookup api and add them to a marketing list if they have a mobile number. The issue i’m running into is checking which lists each contact are in.

The list data is returned thusly:

[
  {
    "static-list-id": 3,
    "internal-list-id": 3,
    "timestamp": 1627415819322,
    "vid": 19769,
    "is-member": true
  },
  {
    "static-list-id": 8,
    "internal-list-id": 10,
    "timestamp": 1633026991979,
    "vid": 19769,
    "is-member": true
  }
]

I have a switch seeing if that array contains the list ID’s im looking for, but it isn’t working for all contacts if they are in a lower numbered list. Is there a way to do this with the switch or am I going to need to preprocess the data with a function?

Full workflow below:

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        50,
        230
      ]
    },
    {
      "parameters": {
        "resource": "contact",
        "operation": "getAll",
        "returnAll": true,
        "additionalFields": {
          "listMerberships": true,
          "properties": [
            "hs_calculated_phone_number_country_code",
            "hs_calculated_phone_number"
          ],
          "propertyMode": "valueOnly"
        }
      },
      "name": "Hubspot",
      "type": "n8n-nodes-base.hubspot",
      "typeVersion": 1,
      "position": [
        200,
        230
      ],
      "credentials": {
        "hubspotApi": "Hubspot account"
      }
    },
    {
      "parameters": {
        "mode": "mergeByIndex"
      },
      "name": "Merge",
      "type": "n8n-nodes-base.merge",
      "typeVersion": 1,
      "position": [
        1060,
        190
      ]
    },
    {
      "parameters": {
        "resource": "contactList",
        "email": "={{$json[\"identity-profiles\"][0][\"identities\"][0][\"value\"]}}",
        "listId": "8"
      },
      "name": "Add to SMS",
      "type": "n8n-nodes-base.hubspot",
      "typeVersion": 1,
      "position": [
        1560,
        30
      ],
      "credentials": {
        "hubspotApi": "Hubspot account"
      }
    },
    {
      "parameters": {
        "resource": "contactList",
        "email": "={{$json[\"identity-profiles\"][0][\"identities\"][0][\"value\"]}}",
        "listId": "9"
      },
      "name": "SMS Invalid",
      "type": "n8n-nodes-base.hubspot",
      "typeVersion": 1,
      "position": [
        1560,
        230
      ],
      "credentials": {
        "hubspotApi": "Hubspot account"
      }
    },
    {
      "parameters": {
        "authentication": "basicAuth",
        "url": "=https://lookups.twilio.com/v1/PhoneNumbers/{{$json[\"properties\"][\"hs_calculated_phone_number\"][\"value\"]}}",
        "options": {
          "useQueryString": true
        },
        "queryParametersUi": {
          "parameter": [
            {
              "name": "CountryCode",
              "value": "={{$json[\"properties\"][\"hs_calculated_phone_number_country_code\"][\"value\"]}}"
            },
            {
              "name": "Type",
              "value": "carrier"
            }
          ]
        }
      },
      "name": "HTTP Request",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        940,
        -10
      ],
      "credentials": {
        "httpBasicAuth": "Unnamed credential"
      }
    },
    {
      "parameters": {
        "dataType": "string",
        "value1": "={{$json[\"list-memberships\"][0][\"static-list-id\"]}}",
        "rules": {
          "rules": [
            {
              "operation": "contains",
              "value2": 8,
              "output": 1
            },
            {
              "operation": "contains",
              "value2": 9,
              "output": 1
            }
          ]
        },
        "fallbackOutput": 0
      },
      "name": "Check List Memberships",
      "type": "n8n-nodes-base.switch",
      "typeVersion": 1,
      "position": [
        570,
        170
      ]
    },
    {
      "parameters": {
        "value1": "={{$json[\"properties\"][\"hs_calculated_phone_number\"][\"value\"]}}",
        "rules": {
          "rules": [
            {
              "operation": "larger"
            }
          ]
        },
        "fallbackOutput": 1
      },
      "name": "Check Phone Number",
      "type": "n8n-nodes-base.switch",
      "typeVersion": 1,
      "position": [
        340,
        230
      ]
    },
    {
      "parameters": {},
      "name": "Already In List",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        700,
        330
      ]
    },
    {
      "parameters": {},
      "name": "No Phone Number",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        450,
        380
      ]
    },
    {
      "parameters": {
        "batchSize": 1,
        "options": {}
      },
      "name": "SplitInBatches",
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 1,
      "position": [
        680,
        -10
      ]
    },
    {
      "parameters": {
        "amount": 0.5,
        "unit": "seconds"
      },
      "name": "Wait",
      "type": "n8n-nodes-base.wait",
      "typeVersion": 1,
      "position": [
        1040,
        -160
      ],
      "webhookId": "87b7bd24-0399-4e7e-a142-bd426612847c"
    },
    {
      "parameters": {},
      "name": "Invalid",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        1460,
        430
      ]
    },
    {
      "parameters": {
        "dataType": "string",
        "value1": "={{$json[\"carrier\"][\"type\"]}}",
        "rules": {
          "rules": [
            {
              "value2": "mobile"
            },
            {
              "value2": "landline",
              "output": 1
            },
            {
              "value2": "voip",
              "output": 1
            },
            {
              "value2": "null",
              "output": 1
            }
          ]
        },
        "fallbackOutput": 2
      },
      "name": "Add to Lists",
      "type": "n8n-nodes-base.switch",
      "typeVersion": 1,
      "position": [
        1220,
        200
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Hubspot",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Hubspot": {
      "main": [
        [
          {
            "node": "Check Phone Number",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge": {
      "main": [
        [
          {
            "node": "Add to Lists",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Request": {
      "main": [
        [
          {
            "node": "Wait",
            "type": "main",
            "index": 0
          },
          {
            "node": "Merge",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Check List Memberships": {
      "main": [
        [
          {
            "node": "SplitInBatches",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Already In List",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Phone Number": {
      "main": [
        [
          {
            "node": "Check List Memberships",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "No Phone Number",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "SplitInBatches": {
      "main": [
        [
          {
            "node": "HTTP Request",
            "type": "main",
            "index": 0
          },
          {
            "node": "Merge",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Wait": {
      "main": [
        [
          {
            "node": "SplitInBatches",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Add to Lists": {
      "main": [
        [
          {
            "node": "Add to SMS",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "SMS Invalid",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Invalid",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Thanks for reading

If I understood correctly, you could probably get it to work with either the Item Lists node or the Function Node. Check the example below:

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "resource": "contact",
        "operation": "getAll",
        "returnAll": true,
        "additionalFields": {
          "listMerberships": true,
          "properties": [
            "hs_calculated_phone_number_country_code",
            "hs_calculated_phone_number"
          ],
          "propertyMode": "valueOnly"
        }
      },
      "name": "Hubspot1",
      "type": "n8n-nodes-base.hubspot",
      "typeVersion": 1,
      "position": [
        330,
        510
      ],
      "credentials": {
        "hubspotApi": "asasas"
      }
    },
    {
      "parameters": {
        "fieldToSplitOut": "list-memberships",
        "include": "selectedOtherFields",
        "fieldsToInclude": {
          "fields": [
            {
              "fieldName": "vid"
            },
            {
              "fieldName": "properties.hs_calculated_phone_number"
            }
          ]
        },
        "options": {}
      },
      "name": "Item Lists",
      "type": "n8n-nodes-base.itemLists",
      "typeVersion": 1,
      "position": [
        550,
        510
      ]
    },
    {
      "parameters": {
        "conditions": {
          "boolean": [
            {
              "value1": "={{ $node[\"Item Lists\"].json[\"properties.hs_calculated_phone_number\"] !== undefined }}",
              "value2": true
            }
          ]
        }
      },
      "name": "IF",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        740,
        510
      ],
      "notesInFlow": true,
      "notes": "has phone"
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Hubspot1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Hubspot1": {
      "main": [
        [
          {
            "node": "Item Lists",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Item Lists": {
      "main": [
        [
          {
            "node": "IF",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Hey Ricardo,
I think that example might help, but what I am trying to do is check all of the memberships and, if they are a member of list 8 or 9, not run my verification code. Its looking like I need to write a function to accomplish this. Thanks for your help!

Ahh yes, I just do not have the Twilio credentials to put together the whole workflow.

Can’t you use an IF node? if internal-list-id equals 8 or equals 9.

I tried that, but list-membership is an array of 2 json objects. It’s seemingly only evaluating the first object. So if a contact is in multiple groups, it only checks the first one.

Yes, because you are only checking the first item of the array. That is why I used the Item list node; to move the items to the main level. That way, you can evaluate all the items instead of only the first one.

Perhaps, the example below helps to understand what I mean.

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        -10,
        830
      ]
    },
    {
      "parameters": {
        "resource": "contact",
        "operation": "getAll",
        "returnAll": true,
        "additionalFields": {
          "listMerberships": true,
          "properties": [
            "hs_calculated_phone_number_country_code",
            "hs_calculated_phone_number"
          ],
          "propertyMode": "valueOnly"
        }
      },
      "name": "Hubspot1",
      "type": "n8n-nodes-base.hubspot",
      "typeVersion": 1,
      "position": [
        170,
        790
      ],
      "credentials": {
        "hubspotApi": "asasas"
      }
    },
    {
      "parameters": {
        "fieldToSplitOut": "list-memberships",
        "include": "selectedOtherFields",
        "fieldsToInclude": {
          "fields": [
            {
              "fieldName": "vid"
            },
            {
              "fieldName": "properties.hs_calculated_phone_number"
            }
          ]
        },
        "options": {}
      },
      "name": "Item Lists",
      "type": "n8n-nodes-base.itemLists",
      "typeVersion": 1,
      "position": [
        420,
        790
      ]
    },
    {
      "parameters": {
        "conditions": {
          "boolean": [
            {
              "value1": "={{ $node[\"Item Lists\"].json[\"properties.hs_calculated_phone_number\"] !== undefined }}",
              "value2": true
            }
          ]
        }
      },
      "name": "IF",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        620,
        790
      ],
      "notesInFlow": true,
      "notes": "has phone"
    },
    {
      "parameters": {
        "conditions": {
          "number": [
            {
              "value1": "={{ $node[\"IF\"].json[\"list-memberships\"][\"static-list-id\"] }}",
              "operation": "larger"
            }
          ]
        }
      },
      "name": "IF1",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        850,
        770
      ]
    },
    {
      "parameters": {},
      "name": "NoOp",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        740,
        1000
      ]
    },
    {
      "parameters": {
        "operation": "removeDuplicates",
        "compare": "selectedFields",
        "fieldsToCompare": {
          "fields": [
            {
              "fieldName": "vid"
            }
          ]
        },
        "options": {}
      },
      "name": "Item Lists1",
      "type": "n8n-nodes-base.itemLists",
      "typeVersion": 1,
      "position": [
        1120,
        750
      ]
    },
    {
      "parameters": {},
      "name": "NoOp1",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        1010,
        1000
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Hubspot1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Hubspot1": {
      "main": [
        [
          {
            "node": "Item Lists",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Item Lists": {
      "main": [
        [
          {
            "node": "IF",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF": {
      "main": [
        [
          {
            "node": "IF1",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "NoOp",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF1": {
      "main": [
        [
          {
            "node": "Item Lists1",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "NoOp1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}