JSONata is true but switch node will not pass msg through

Hi -

I'm new to Node-RED and am trying to use JSONata in a Switch node to route my msg.payload. I found this in my search and attempted to replicate it for my data. Though the JSONata Test reports True, my payload goes no further. The JSON is a vm_info including tags created by Ansible..

My attempt to replicate the example in the linked post with JSONata in the Property field:

true in tags[*].$contains($.name, "AnsibleManaged")

ref: JSONata Exerciser

Additionally, I have tried the following syntax as a JSONata rule:

"AnsibleManaged" in tags.name

ref: JSONata Exerciser

and

true in tags[*].$contains($.name, "AnsibleManaged")

... these both evaluate to True and pass the test against my JSON.

I'm obviously not doing something correctly here and would appreciate any help. Thanks!

[{"id":"c82f6541f05e4529","type":"tab","label":"Debug with Functions","disabled":false,"info":"","env":[]},{"id":"b5ec2a3d5867fecd","type":"inject","z":"c82f6541f05e4529","name":"Send Test vm_info Payload","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"guest_name\":\"DebugNode-RED\",\"guest_fullname\":\"Ubuntu Linux (64-bit)\",\"power_state\":\"poweredOn\",\"ip_address\":\"192.168.50.254\",\"mac_address\":[\"00:50:54:87:bc:74\"],\"uuid\":\"4207b810-fc10-a2ad-dd5d-ced4ba83d953\",\"vm_network\":{\"00:50:56:87:bc:74\":{\"ipv4\":[\"192.168.50.254\"],\"ipv6\":[\"fe80::250:54ff:fe87:bc74\"]}},\"esxi_hostname\":\"massstorage.homelab.bjzy.me\",\"datacenter\":\"Home Lab Datacenter\",\"cluster\":\"Home Lab CL1\",\"resource_pool\":null,\"attributes\":{},\"tags\":[{\"id\":\"urn:vmomi:InventoryServiceTag:1c7ec29c-cfcf-4e81-8bc5-043505a73616:GLOBAL\",\"category_name\":\"Automation\",\"name\":\"AnsibleManaged\",\"description\":\"Ansible is used to configure this resource\",\"category_id\":\"urn:vmomi:InventoryServiceCategory:dcf78d63-1f5b-412f-87e6-35c17fe7e363:GLOBAL\"},{\"id\":\"urn:vmomi:InventoryServiceTag:0fb3a150-093d-4415-9465-ceacce0f28e1:GLOBAL\",\"category_name\":\"Automation\",\"name\":\"Terraform\",\"description\":\"Indicates VM was created and state is managed by Terraform.\",\"category_id\":\"urn:vmomi:InventoryServiceCategory:dcf78d63-1f5b-412f-87e6-35c17fe7e363:GLOBAL\"},{\"id\":\"urn:vmomi:InventoryServiceTag:f4360074-ff82-46f2-b7da-6fe1bd91defa:GLOBAL\",\"category_name\":\"Environment\",\"name\":\"Development\",\"description\":\"Formal Dev environment. Build and implement here first.\",\"category_id\":\"urn:vmomi:InventoryServiceCategory:a968e543-f640-4f6e-a9ee-cd3c5bc234ee:GLOBAL\"},{\"id\":\"urn:vmomi:InventoryServiceTag:52b62f3e-ca99-4416-ab0f-da6b4eca462d:GLOBAL\",\"category_name\":\"Monitoring\",\"name\":\"bootNotify\",\"description\":\"Host should send an alert on boot. This tag will trigger bootNotify install.\",\"category_id\":\"urn:vmomi:InventoryServiceCategory:6cd83cd3-adfd-4247-8776-322a5ff233ff:GLOBAL\"}],\"folder\":\"/Home Lab Datacenter/vm\",\"moid\":\"vm-25928\",\"datastore_url\":[{\"name\":\"NVMe-c\",\"url\":\"/vmfs/volumes/60bd10bb-fe1c3d76-e39d-0cc47a172710\"}],\"allocated\":{}}","payloadType":"json","x":230,"y":180,"wires":[["f43467b9f14d281b"]]},{"id":"f43467b9f14d281b","type":"switch","z":"c82f6541f05e4529","name":"Tag Flow Control [JSONata Function Rule]","property":"payload","propertyType":"msg","rules":[{"t":"jsonata_exp","v":"true in tags[*].$contains($.name, \"AnsibleManaged\")","vt":"jsonata"}],"checkall":"true","repair":false,"outputs":1,"x":560,"y":180,"wires":[["044e3cebb1590862"]]},{"id":"044e3cebb1590862","type":"debug","z":"c82f6541f05e4529","name":"Debug","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":910,"y":180,"wires":[]},{"id":"69a0012070b5be7f","type":"inject","z":"c82f6541f05e4529","name":"Send Test vm_info Payload","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"guest_name\":\"DebugNode-RED\",\"guest_fullname\":\"Ubuntu Linux (64-bit)\",\"power_state\":\"poweredOn\",\"ip_address\":\"192.168.50.254\",\"mac_address\":[\"00:50:54:87:bc:74\"],\"uuid\":\"4207b810-fc10-a2ad-dd5d-ced4ba83d953\",\"vm_network\":{\"00:50:56:87:bc:74\":{\"ipv4\":[\"192.168.50.254\"],\"ipv6\":[\"fe80::250:54ff:fe87:bc74\"]}},\"esxi_hostname\":\"massstorage.homelab.bjzy.me\",\"datacenter\":\"Home Lab Datacenter\",\"cluster\":\"Home Lab CL1\",\"resource_pool\":null,\"attributes\":{},\"tags\":[{\"id\":\"urn:vmomi:InventoryServiceTag:1c7ec29c-cfcf-4e81-8bc5-043505a73616:GLOBAL\",\"category_name\":\"Automation\",\"name\":\"AnsibleManaged\",\"description\":\"Ansible is used to configure this resource\",\"category_id\":\"urn:vmomi:InventoryServiceCategory:dcf78d63-1f5b-412f-87e6-35c17fe7e363:GLOBAL\"},{\"id\":\"urn:vmomi:InventoryServiceTag:0fb3a150-093d-4415-9465-ceacce0f28e1:GLOBAL\",\"category_name\":\"Automation\",\"name\":\"Terraform\",\"description\":\"Indicates VM was created and state is managed by Terraform.\",\"category_id\":\"urn:vmomi:InventoryServiceCategory:dcf78d63-1f5b-412f-87e6-35c17fe7e363:GLOBAL\"},{\"id\":\"urn:vmomi:InventoryServiceTag:f4360074-ff82-46f2-b7da-6fe1bd91defa:GLOBAL\",\"category_name\":\"Environment\",\"name\":\"Development\",\"description\":\"Formal Dev environment. Build and implement here first.\",\"category_id\":\"urn:vmomi:InventoryServiceCategory:a968e543-f640-4f6e-a9ee-cd3c5bc234ee:GLOBAL\"},{\"id\":\"urn:vmomi:InventoryServiceTag:52b62f3e-ca99-4416-ab0f-da6b4eca462d:GLOBAL\",\"category_name\":\"Monitoring\",\"name\":\"bootNotify\",\"description\":\"Host should send an alert on boot. This tag will trigger bootNotify install.\",\"category_id\":\"urn:vmomi:InventoryServiceCategory:6cd83cd3-adfd-4247-8776-322a5ff233ff:GLOBAL\"}],\"folder\":\"/Home Lab Datacenter/vm\",\"moid\":\"vm-25928\",\"datastore_url\":[{\"name\":\"NVMe-c\",\"url\":\"/vmfs/volumes/60bd10bb-fe1c3d76-e39d-0cc47a172710\"}],\"allocated\":{}}","payloadType":"json","x":230,"y":280,"wires":[["647ed971a5cc398e"]]},{"id":"647ed971a5cc398e","type":"switch","z":"c82f6541f05e4529","name":"Tag Flow Control [JSONata Property]","property":"true in tags[*].$contains($.name, \"AnsibleManaged\")","propertyType":"jsonata","rules":[{"t":"true"}],"checkall":"true","repair":false,"outputs":1,"x":550,"y":280,"wires":[["92992e64d5c23f3b"]]},{"id":"92992e64d5c23f3b","type":"debug","z":"c82f6541f05e4529","name":"Debug","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":870,"y":280,"wires":[]},{"id":"a7b171a4741c25fb","type":"inject","z":"c82f6541f05e4529","name":"Send Test vm_info Payload","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"guest_name\":\"DebugNode-RED\",\"guest_fullname\":\"Ubuntu Linux (64-bit)\",\"power_state\":\"poweredOn\",\"ip_address\":\"192.168.50.254\",\"mac_address\":[\"00:50:54:87:bc:74\"],\"uuid\":\"4207b810-fc10-a2ad-dd5d-ced4ba83d953\",\"vm_network\":{\"00:50:56:87:bc:74\":{\"ipv4\":[\"192.168.50.254\"],\"ipv6\":[\"fe80::250:54ff:fe87:bc74\"]}},\"esxi_hostname\":\"massstorage.homelab.bjzy.me\",\"datacenter\":\"Home Lab Datacenter\",\"cluster\":\"Home Lab CL1\",\"resource_pool\":null,\"attributes\":{},\"tags\":[{\"id\":\"urn:vmomi:InventoryServiceTag:1c7ec29c-cfcf-4e81-8bc5-043505a73616:GLOBAL\",\"category_name\":\"Automation\",\"name\":\"AnsibleManaged\",\"description\":\"Ansible is used to configure this resource\",\"category_id\":\"urn:vmomi:InventoryServiceCategory:dcf78d63-1f5b-412f-87e6-35c17fe7e363:GLOBAL\"},{\"id\":\"urn:vmomi:InventoryServiceTag:0fb3a150-093d-4415-9465-ceacce0f28e1:GLOBAL\",\"category_name\":\"Automation\",\"name\":\"Terraform\",\"description\":\"Indicates VM was created and state is managed by Terraform.\",\"category_id\":\"urn:vmomi:InventoryServiceCategory:dcf78d63-1f5b-412f-87e6-35c17fe7e363:GLOBAL\"},{\"id\":\"urn:vmomi:InventoryServiceTag:f4360074-ff82-46f2-b7da-6fe1bd91defa:GLOBAL\",\"category_name\":\"Environment\",\"name\":\"Development\",\"description\":\"Formal Dev environment. Build and implement here first.\",\"category_id\":\"urn:vmomi:InventoryServiceCategory:a968e543-f640-4f6e-a9ee-cd3c5bc234ee:GLOBAL\"},{\"id\":\"urn:vmomi:InventoryServiceTag:52b62f3e-ca99-4416-ab0f-da6b4eca462d:GLOBAL\",\"category_name\":\"Monitoring\",\"name\":\"bootNotify\",\"description\":\"Host should send an alert on boot. This tag will trigger bootNotify install.\",\"category_id\":\"urn:vmomi:InventoryServiceCategory:6cd83cd3-adfd-4247-8776-322a5ff233ff:GLOBAL\"}],\"folder\":\"/Home Lab Datacenter/vm\",\"moid\":\"vm-25928\",\"datastore_url\":[{\"name\":\"NVMe-c\",\"url\":\"/vmfs/volumes/60bd10bb-fe1c3d76-e39d-0cc47a172710\"}],\"allocated\":{}}","payloadType":"json","x":210,"y":380,"wires":[["4f6f79d376800ade"]]},{"id":"4f6f79d376800ade","type":"switch","z":"c82f6541f05e4529","name":"Tag Flow Control [JSONata Rule w/o Functions]","property":"payload","propertyType":"msg","rules":[{"t":"jsonata_exp","v":"\"AnsibleManaged\" in tags.name","vt":"jsonata"}],"checkall":"true","repair":false,"outputs":1,"x":560,"y":380,"wires":[["655916a4baf08d8b"]]},{"id":"655916a4baf08d8b","type":"debug","z":"c82f6541f05e4529","name":"Debug","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":890,"y":380,"wires":[]}]
{
    "guest_name": "DebugNode-RED",
    "guest_fullname": "Ubuntu Linux (64-bit)",
    "power_state": "poweredOn",
    "ip_address": "192.168.50.254",
    "mac_address": [
        "00:50:54:87:bc:74"
    ],
    "uuid": "4207b810-fc10-a2ad-dd5d-ced4ba83d953",
    "vm_network": {
        "00:50:56:87:bc:74": {
            "ipv4": [
                "192.168.50.254"
            ],
            "ipv6": [
                "fe80::250:54ff:fe87:bc74"
            ]
        }
    },
    "esxi_hostname": "massstorage.homelab.bjzy.me",
    "datacenter": "Home Lab Datacenter",
    "cluster": "Home Lab CL1",
    "resource_pool": null,
    "attributes": {},
    "tags": [
        {
            "id": "urn:vmomi:InventoryServiceTag:1c7ec29c-cfcf-4e81-8bc5-043505a73616:GLOBAL",
            "category_name": "Automation",
            "name": "AnsibleManaged",
            "description": "Ansible is used to configure this resource",
            "category_id": "urn:vmomi:InventoryServiceCategory:dcf78d63-1f5b-412f-87e6-35c17fe7e363:GLOBAL"
        },
        {
            "id": "urn:vmomi:InventoryServiceTag:0fb3a150-093d-4415-9465-ceacce0f28e1:GLOBAL",
            "category_name": "Automation",
            "name": "Terraform",
            "description": "Indicates VM was created and state is managed by Terraform.",
            "category_id": "urn:vmomi:InventoryServiceCategory:dcf78d63-1f5b-412f-87e6-35c17fe7e363:GLOBAL"
        },
        {
            "id": "urn:vmomi:InventoryServiceTag:f4360074-ff82-46f2-b7da-6fe1bd91defa:GLOBAL",
            "category_name": "Environment",
            "name": "Development",
            "description": "Formal Dev environment. Build and implement here first.",
            "category_id": "urn:vmomi:InventoryServiceCategory:a968e543-f640-4f6e-a9ee-cd3c5bc234ee:GLOBAL"
        },
        {
            "id": "urn:vmomi:InventoryServiceTag:52b62f3e-ca99-4416-ab0f-da6b4eca462d:GLOBAL",
            "category_name": "Monitoring",
            "name": "bootNotify",
            "description": "Host should send an alert on boot. This tag will trigger bootNotify install.",
            "category_id": "urn:vmomi:InventoryServiceCategory:6cd83cd3-adfd-4247-8776-322a5ff233ff:GLOBAL"
        }
    ],
    "folder": "/Home Lab Datacenter/vm",
    "moid": "vm-25928",
    "datastore_url": [
        {
            "name": "NVMe-c",
            "url": "/vmfs/volumes/60bd10bb-fe1c3d76-e39d-0cc47a172710"
        }
    ],
    "allocated": {}
}

cc @ E1cid

1 Like

Your array is in payload.tags
So would need to append payload to your expressions, or use wildcards * or descendants **

[{"id":"b5ec2a3d5867fecd","type":"inject","z":"c82f6541f05e4529","name":"Send Test vm_info Payload","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"guest_name\":\"DebugNode-RED\",\"guest_fullname\":\"Ubuntu Linux (64-bit)\",\"power_state\":\"poweredOn\",\"ip_address\":\"192.168.50.254\",\"mac_address\":[\"00:50:54:87:bc:74\"],\"uuid\":\"4207b810-fc10-a2ad-dd5d-ced4ba83d953\",\"vm_network\":{\"00:50:56:87:bc:74\":{\"ipv4\":[\"192.168.50.254\"],\"ipv6\":[\"fe80::250:54ff:fe87:bc74\"]}},\"esxi_hostname\":\"massstorage.homelab.bjzy.me\",\"datacenter\":\"Home Lab Datacenter\",\"cluster\":\"Home Lab CL1\",\"resource_pool\":null,\"attributes\":{},\"tags\":[{\"id\":\"urn:vmomi:InventoryServiceTag:1c7ec29c-cfcf-4e81-8bc5-043505a73616:GLOBAL\",\"category_name\":\"Automation\",\"name\":\"AnsibleManaged\",\"description\":\"Ansible is used to configure this resource\",\"category_id\":\"urn:vmomi:InventoryServiceCategory:dcf78d63-1f5b-412f-87e6-35c17fe7e363:GLOBAL\"},{\"id\":\"urn:vmomi:InventoryServiceTag:0fb3a150-093d-4415-9465-ceacce0f28e1:GLOBAL\",\"category_name\":\"Automation\",\"name\":\"Terraform\",\"description\":\"Indicates VM was created and state is managed by Terraform.\",\"category_id\":\"urn:vmomi:InventoryServiceCategory:dcf78d63-1f5b-412f-87e6-35c17fe7e363:GLOBAL\"},{\"id\":\"urn:vmomi:InventoryServiceTag:f4360074-ff82-46f2-b7da-6fe1bd91defa:GLOBAL\",\"category_name\":\"Environment\",\"name\":\"Development\",\"description\":\"Formal Dev environment. Build and implement here first.\",\"category_id\":\"urn:vmomi:InventoryServiceCategory:a968e543-f640-4f6e-a9ee-cd3c5bc234ee:GLOBAL\"},{\"id\":\"urn:vmomi:InventoryServiceTag:52b62f3e-ca99-4416-ab0f-da6b4eca462d:GLOBAL\",\"category_name\":\"Monitoring\",\"name\":\"bootNotify\",\"description\":\"Host should send an alert on boot. This tag will trigger bootNotify install.\",\"category_id\":\"urn:vmomi:InventoryServiceCategory:6cd83cd3-adfd-4247-8776-322a5ff233ff:GLOBAL\"}],\"folder\":\"/Home Lab Datacenter/vm\",\"moid\":\"vm-25928\",\"datastore_url\":[{\"name\":\"NVMe-c\",\"url\":\"/vmfs/volumes/60bd10bb-fe1c3d76-e39d-0cc47a172710\"}],\"allocated\":{}}","payloadType":"json","x":230,"y":180,"wires":[["f43467b9f14d281b","785ea0b8cbe9af91","4f6f79d376800ade"]]},{"id":"f43467b9f14d281b","type":"switch","z":"c82f6541f05e4529","name":"Tag Flow Control [JSONata Function Rule]","property":"true in *.tags.$contains($.name, \"AnsibleManaged\")","propertyType":"jsonata","rules":[{"t":"true"}],"checkall":"true","repair":false,"outputs":1,"x":560,"y":180,"wires":[["044e3cebb1590862"]]},{"id":"785ea0b8cbe9af91","type":"switch","z":"c82f6541f05e4529","name":"Tag Flow Control [JSONata Function Rule]","property":"payload","propertyType":"msg","rules":[{"t":"jsonata_exp","v":"true in $$.payload.tags.$contains($.name, \"AnsibleManaged\")","vt":"jsonata"}],"checkall":"true","repair":false,"outputs":1,"x":580,"y":260,"wires":[["31608d77844d3ea0"]]},{"id":"4f6f79d376800ade","type":"switch","z":"c82f6541f05e4529","name":"Tag Flow Control [JSONata Rule w/o Functions]","property":"payload","propertyType":"msg","rules":[{"t":"jsonata_exp","v":"\"AnsibleManaged\" in $$.payload.tags.name","vt":"jsonata"}],"checkall":"true","repair":false,"outputs":1,"x":560,"y":380,"wires":[["655916a4baf08d8b"]]},{"id":"044e3cebb1590862","type":"debug","z":"c82f6541f05e4529","name":"Debug","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":910,"y":180,"wires":[]},{"id":"31608d77844d3ea0","type":"debug","z":"c82f6541f05e4529","name":"Debug","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":890,"y":280,"wires":[]},{"id":"655916a4baf08d8b","type":"debug","z":"c82f6541f05e4529","name":"Debug","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":890,"y":380,"wires":[]}]

p.s. Love the full info and examples provided

[edit]
Your jsonata exerciser is missing the payload element to
should be

{
  "payload": {
    "allocated": {},
    "attributes": {},
    "cluster": "Home Lab CL1",
    "datacenter": "Home Lab Datacenter",
    "datastore_url": [
      {
        "name": "NVMe-c",
        "url": "/vmfs/volumes/60bd10bb-fe1c3d76-e39d-0cc47a172710"
      }
    ],
    "esxi_hostname": "massstorage.homelab.bjzy.me",
    "folder": "/Home Lab Datacenter/vm",
    "guest_fullname": "Ubuntu Linux (64-bit)",
    "guest_name": "DebugNode-RED",
    "ip_address": "192.168.50.254",
    "mac_address": [
      "00:50:54:87:bc:74"
    ],
    "moid": "vm-25928",
    "power_state": "poweredOn",
    "resource_pool": null,
    "tags": [
      {
        "category_id": "urn:vmomi:InventoryServiceCategory:dcf78d63-1f5b-412f-87e6-35c17fe7e363:GLOBAL",
        "category_name": "Automation",
        "description": "Ansible is used to configure this resource",
        "id": "urn:vmomi:InventoryServiceTag:1c7ec29c-cfcf-4e81-8bc5-043505a73616:GLOBAL",
        "name": "AnsibleManaged"
      },
      {
        "category_id": "urn:vmomi:InventoryServiceCategory:dcf78d63-1f5b-412f-87e6-35c17fe7e363:GLOBAL",
        "category_name": "Automation",
        "description": "Indicates VM was created and state is managed by Terraform.",
        "id": "urn:vmomi:InventoryServiceTag:0fb3a150-093d-4415-9465-ceacce0f28e1:GLOBAL",
        "name": "Terraform"
      },
      {
        "category_id": "urn:vmomi:InventoryServiceCategory:a968e543-f640-4f6e-a9ee-cd3c5bc234ee:GLOBAL",
        "category_name": "Environment",
        "description": "Formal Dev environment. Build and implement here first.",
        "id": "urn:vmomi:InventoryServiceTag:f4360074-ff82-46f2-b7da-6fe1bd91defa:GLOBAL",
        "name": "Development"
      },
      {
        "category_id": "urn:vmomi:InventoryServiceCategory:6cd83cd3-adfd-4247-8776-322a5ff233ff:GLOBAL",
        "category_name": "Monitoring",
        "description": "Host should send an alert on boot. This tag will trigger bootNotify install.",
        "id": "urn:vmomi:InventoryServiceTag:52b62f3e-ca99-4416-ab0f-da6b4eca462d:GLOBAL",
        "name": "bootNotify"
      }
    ],
    "uuid": "4207b810-fc10-a2ad-dd5d-ced4ba83d953",
    "vm_network": {
      "00:50:56:87:bc:74": {
        "ipv4": [
          "192.168.50.254"
        ],
        "ipv6": [
          "fe80::250:54ff:fe87:bc74"
        ]
      }
    }
  }
}

Then your inject nodes will match the exerciser objects.

1 Like

Everything is working as expected now. Thanks for the help!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.