Validation by matching 2 json and the route vi if

HI

I don’t know if this is possible to achieve or even if it understandable, I have tried a great number of different solutions, but I fell entirely lost in this case. I have tried with if nodes etc which i have attached but most of it is faulty

I am trying to make a quite complex routing by having an incoming that should get validated against another json and then be routed via the if node

“Example1”

I want the incoming json that contains an array of a number of subitems get validated as following

Example 1
“inEvent”.“topic”.“Id”= “[policynode].[topic.id]”
and
“inEvent”.“location”.“Id”= “[policynode].[location.id]”
and
“contextData”.“euqipment"id”=" “[policynode].[equipment.id]”
and
“count” “contextData”.“euqipment"id” >= “[policynode].[instances]”

If all conditions are met then it is “true”

“Example2”

I want to validate if a number value in the inEvent is eqaul or grater than the “policyData”

“inEvent”.“topic”.“Id”= “[policynode].[topic.id]”
and
“inEvent”.“location”.“Id”= “[policynode].[location.id]”
and
“contextData”.“enviroment”.“winds”.“speed”>= “[policynode].[wind"speed]”

“Example3”
Same as exmaple 2 but with other proporties
“inEvent”.“topic”.“Id”= “[policynode].[topic.id]”
and
“inEvent”.“location”.“Id”= “[policynode].[location.id]”
and
“contextData”.“enviroment”.“current”.“speed”>= "[policynode].(value2)

“exmaple” 4
“inEvent”.“topic”.“Id”= “[policynode].[topic.id]”
and
“inEvent”.“location”.“Id”= “[policynode].[location.id]”
and
“contextData”.“euqipment”.“id”=" “[policynode].[equipment.id]”
and
[
where instance with 1 id 1 is eual to the instance with id 1 in the policy

]

{
  "nodes": [
    {
      "parameters": {
        "functionCode": "const data = [\n{\n\"topic\":{\n\"id\": \"exampleEvent1\"\n },\n\"locationdata\":{\n  \"id\": \"Location1\"\n},\n\"contextData\":{\n  \"equipment\": \n  {\n    \"id\": \"equipment1\",\n    \"instances\":[\n    {\n      \"id\": \"i1\",\n      \"state\": \"active\"\n    },\n    {\n      \"id\": \"i2\",\n      \"state\": \"inactive\"\n    },\n    {\n      \"id\": \"i3\",\n      \"state\": \"active\"\n    }\n    ]\n  }\n}\n},\n{\n\"topic\":{\n\"id\": \"exampleEvent2\"\n},\n\"locationdata\":{\n  \"id\": \"Location1\"\n},\n\"contextData\":{\n  \"environment\": \n  {\n    \"wind\":{\n      \"speed\": \"10\",\n      \"format\": \"m/s\"\n    }\n  }\n}\n},\n{\n\"topic\":{\n\"id\": \"exampleEvent3\"\n},\n\"contextData\":{\n  \"environment\": \n  {\n    \"current\":{\n      \"speed\": \"10\",\n      \"format\": \"m/s\"\n    }\n  }\n}\n},\n{\n\"topic\":{\n\"id\": \"exampleEvent4\"\n},\n\"locationdata\":{\n  \"id\": \"Location1\"\n},\n\"contextData\":{\n  \"equipment\": \n  {\n    \"id\": \"equipment1\",\n    \"instances\":[\n    {\n      \"id\": \"i1\",\n      \"state\": \"active\"\n    },\n    {\n      \"id\": \"i2\",\n      \"state\": \"active\"\n    },\n    {\n      \"id\": \"i3\",\n      \"state\": \"inactive\"\n    }\n    ]\n  }\n}\n}\n];\n\nconst eventId = $node[\"setTopicId\"].json[\"topicid\"]\nconst items = data.filter(entry => {\n    let isValid = true;\n    isValid = entry.topic.id === eventId ? isValid : false;\n    return isValid;\n});\n\nconst newItems = [];\nitems.forEach(item => newItems.push({\n    json: item\n}));\nreturn newItems;\n"
      },
      "name": "inEvent",
      "type": "n8n-nodes-base.function",
      "position": [
        650,
        300
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "functionCode": "const data = [\n{\n\"topic\":{\n\"id\": \"exampleEvent1\"\n},\n\"locationdata\":{\n  \"id\": \"Location1\"\n},\n\"validationdata\":{\n  \"id\": \"\",\n  \"policy\": \"and'$['inEvent'].json['contextData'].['eqipment'].['id']='equipment1','$['inEvent'].json['contextData'].['eqipment'].['intances']count???.....[state]=active > 2\",\n  \"messege\":{\n  \"true\": \"'Accepted'.concat($[inEvent].json[contextData].[equipment].[instances])\",\n  \"false\": \"'Not accepted'.concat($[inEvent].json[contextData].[equipment].[instances])\"\n  }\n}\n},\n\n{\n\"topic\":{\n\"id\": \"exampleEvent2\"\n},\n\"locationdata\":{\n  \"id\": \"Location1\"\n},\n\"validationdata\":{\n  \"policy\":  \"$['inEvent'].json['contextData'].['environment'].['wind'].['speed'] => 2\",\n  \"messege\":{\n  \"true\": \"'Accepted'.concat($['inEvent'].json['contextData'].['environment'].['wind'].['speed'])\",\n  \"false\": \"'Not accepted'concat.($['inEvent'].json['contextData'].['environment'].['wind'].['speed'])\"\n  }\n}\n},\n\n{\n\"topic\":{\n\"id\": \"exampleEvent3\"\n},\n\"locationdata\":{\n  \"id\": \"Location1\"\n},\n\"validationdata\":{\n  \"policy\":  \"$['inEvent'].json['contextData'].['enviroment'].['current'].['speed'] => 2\",\n  \"messege\":{\n  \"true\": \"'Accepted'.concat($['inEvent'].json['contextData'].['environment'].['current'].['speed'])\",\n  \"false\": \"'Not accepted'concat.($['inEvent'].json['contextData'].['environment'].['current'].['speed'])\"\n  }\n}\n},\n{\n\"topic\":{\n\"id\": \"exampleEvent4\"\n},\n\"locationdata\":{\n  \"id\": \"Location1\"\n},\n\"validationdata\":{\n  \"policy\":  \"here  should contextdata.equipment.id = equipment1, and (instance with id = i1 should have value 'state'='active'  and instance with id i2 should be 'state'='active' and id i3 should have stateto 'state''=''inactive' in ordet to be true\",\n  \"messege\":{\n  \"true\": \"'Accepted'.concat($[inEvent].json[contextData].[equipment].[instances])\",\n  \"false\": \"'Not accepted'.concat($[inEvent].json[contextData].[equipment].[instances])\"\n  }\n}\n}\n\n];\n\nconst eventId = $node[\"setTopicId\"].json[\"topicid\"]\nconst items = data.filter(entry => {\n    let isValid = true;\n    isValid = entry.topic.id === eventId ? isValid : false;\n    return isValid;\n});\n\nconst newItems = [];\nitems.forEach(item => newItems.push({\n    json: item\n}));\nreturn newItems;\n"
      },
      "name": "policy",
      "type": "n8n-nodes-base.function",
      "position": [
        840,
        300
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "functionCode": "\nfunction resolveData(data) {\n  if (data !== null && typeof data === 'object') {\n    const returnData = {};\n    for (const key of Object.keys(data)) {\n    console.log('key: ' + key);\n      if (key === '_id') {\n        returnData[key] = data[key].toString();\n        continue;\n      }\n      returnData[key] = resolveData(data[key]);\n    }\n    return returnData;\n  }\n\n  if (typeof data === 'string' && data.charAt(0) === '{') {\n    return $evaluateExpression(data);\n  } \n  \n  return data;\n}\n\nreturn [\n  {\n    json: resolveData($node[\"mcEvents\"].json),\n  }\n]\n\n"
      },
      "name": "Convert",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        1050,
        300
      ]
    }
  ],
  "connections": {
    "inEvent": {
      "main": [
        [
          {
            "node": "policy",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "policy": {
      "main": [
        [
          {
            "node": "Convert",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Sadly do your nodes not work. So I tried to repair it and get them working. Please make always first sure that your example workflows really work, that we do not have to spend time on debugging them first. Thanks!

Also does your example not seem to be complete as I can not find any “euqipment” or “equipment” anyway so only post here now an example workflow with IF for “Example 1” and only the first two conditions as that are the only ones that exist. Hope it is helpful anyway as also the other ones would work exactly the same:

{
  "nodes": [
    {
      "parameters": {
        "functionCode": "const data = [\n{\n\"topic\":{\n\"id\": \"exampleEvent1\"\n },\n\"locationdata\":{\n  \"id\": \"Location1\"\n},\n\"contextData\":{\n  \"equipment\": \n  {\n    \"id\": \"equipment1\",\n    \"instances\":[\n    {\n      \"id\": \"i1\",\n      \"state\": \"active\"\n    },\n    {\n      \"id\": \"i2\",\n      \"state\": \"inactive\"\n    },\n    {\n      \"id\": \"i3\",\n      \"state\": \"active\"\n    }\n    ]\n  }\n}\n},\n{\n\"topic\":{\n\"id\": \"exampleEvent2\"\n},\n\"locationdata\":{\n  \"id\": \"Location1\"\n},\n\"contextData\":{\n  \"enviroment\": \n  {\n    \"wind\":{\n      \"speed\": \"10\",\n      \"format\": \"m/s\"\n    }\n  }\n}\n},\n{\n\"topic\":{\n\"id\": \"exampleEvent3\"\n},\n\"contextData\":{\n  \"enviroment\": \n  {\n    \"current\":{\n      \"speed\": \"10\",\n      \"format\": \"m/s\"\n    }\n  }\n}\n},\n{\n\"topic\":{\n\"id\": \"exampleEvent4\"\n},\n\"locationdata\":{\n  \"id\": \"Location1\"\n},\n\"contextData\":{\n  \"equipment\": \n  {\n    \"id\": \"equipment1\",\n    \"instances\":[\n    {\n      \"id\": \"i1\",\n      \"state\": \"active\"\n    },\n    {\n      \"id\": \"i2\",\n      \"state\": \"active\"\n    },\n    {\n      \"id\": \"i3\",\n      \"state\": \"inactive\"\n    }\n    ]\n  }\n}\n}\n];\n\nconst eventId = $node[\"setTopicId\"].json[\"topicid\"]\nconst items = data.filter(entry => {\n    let isValid = true;\n    isValid = entry.topic.id === eventId ? isValid : false;\n    return isValid;\n});\n\nconst newItems = [];\nitems.forEach(item => newItems.push({\n    json: item\n}));\nreturn newItems;\n"
      },
      "name": "inEvent",
      "type": "n8n-nodes-base.function",
      "position": [
        650,
        300
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "functionCode": "const data = [\n{\n\"topic\":{\n\"id\": \"exampleEvent1\"\n},\n\"locationdata\":{\n  \"id\": \"Location1\"\n},\n\"validationdata\":{\n  \"id\": \"\",\n  \"policy\": \"and'$['inEvent'].json['contextData'].['eqipment'].['id']='equipment1','$['inEvent'].json['contextData'].['eqipment'].['intances']count???.....[state]=active > 2\",\n  \"messege\":{\n  \"true\": \"'Accepted'.concat($[inEvent].json[contextData].[equipment].[instances])\",\n  \"false\": \"'Not accepted'.concat($[inEvent].json[contextData].[equipment].[instances])\"\n  }\n}\n},\n\n{\n\"topic\":{\n\"id\": \"exampleEvent2\"\n},\n\"locationdata\":{\n  \"id\": \"Location1\"\n},\n\"validationdata\":{\n  \"policy\":  \"$['inEvent'].json['contextData'].['enviroment'].['wind'].['speed'] => 2\",\n  \"messege\":{\n  \"true\": \"'Accepted'.concat($['inEvent'].json['contextData'].['enviroment'].['wind'].['speed'])\",\n  \"false\": \"'Not accepted'concat.($['inEvent'].json['contextData'].['enviroment'].['wind'].['speed'])\"\n  }\n}\n},\n\n{\n\"topic\":{\n\"id\": \"exampleEvent3\"\n},\n\"locationdata\":{\n  \"id\": \"Location1\"\n},\n\"validationdata\":{\n  \"policy\":  \"$['inEvent'].json['contextData'].['enviroment'].['current'].['speed'] => 2\",\n  \"messege\":{\n  \"true\": \"'Accepted'.concat($['inEvent'].json['contextData'].['enviroment'].['current'].['speed'])\",\n  \"false\": \"'Not accepted'concat.($['inEvent'].json['contextData'].['enviroment'].['current'].['speed'])\"\n  }\n}\n},\n{\n\"topic\":{\n\"id\": \"exampleEvent4\"\n},\n\"locationdata\":{\n  \"id\": \"Location1\"\n},\n\"validationdata\":{\n  \"policy\":  \"here  should contextdata.equipment.id = equipment1, and (instance with id = i1 should have value 'state'='active'  and instance with id i2 should be 'state'='active' and id i3 should have stateto 'state''=''inactive' in ordet to be true\",\n  \"messege\":{\n  \"true\": \"'Accepted'.concat($[inEvent].json[contextData].[equipment].[instances])\",\n  \"false\": \"'Not accepted'.concat($[inEvent].json[contextData].[equipment].[instances])\"\n  }\n}\n}\n\n];\n\nconst eventId = $node[\"setTopicId\"].json[\"topicid\"]\nconst items = data.filter(entry => {\n    let isValid = true;\n    isValid = entry.topic.id === eventId ? isValid : false;\n    return isValid;\n});\n\nconst newItems = [];\nitems.forEach(item => newItems.push({\n    json: item\n}));\nreturn newItems;\n"
      },
      "name": "policy",
      "type": "n8n-nodes-base.function",
      "position": [
        850,
        300
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "topicid",
              "value": "exampleEvent2"
            }
          ]
        },
        "options": {}
      },
      "name": "setTopicId",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{$node[\"inEvent\"].json[\"topic\"][\"id\"]}}",
              "value2": "={{$node[\"policy\"].json[\"topic\"][\"id\"]}}"
            },
            {
              "value1": "={{$node[\"inEvent\"].json[\"locationdata\"][\"id\"]}}",
              "value2": "={{$node[\"policy\"].json[\"locationdata\"][\"id\"]}}"
            }
          ]
        }
      },
      "name": "IF",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        1050,
        300
      ]
    }
  ],
  "connections": {
    "inEvent": {
      "main": [
        [
          {
            "node": "policy",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "policy": {
      "main": [
        [
          {
            "node": "IF",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "setTopicId": {
      "main": [
        [
          {
            "node": "inEvent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Hi Jan

Thanks for you answer, i did my self get that far with the topic id and the ship id but the challenge comes with the calculating policy

The thing is that I need more complex validation by having a formula added to the policy node.

In this I example I would like to add a formula that gets calculated where it formula counts how many instances there active and match them against a value and if they are eg = 2 or more then it is true.

input data

.

trail of policy

{
"topic":{
"id": "exampleEvent1"
},
"locationdata":{
  "id": "Location1"
},
"validationdata":{
  "id": "",
  "policy": "and'$['inEvent'].json['contextData'].['eqipment'].['id']'equipment1','$['inEvent'].json['contextData'].['eqipment'].['intances']count???.....[state]=active > 2",
  "messege":{
  "true": "'Accepted'.concat($[inEvent].json[contextData].[equipment].[instances])",
  "false": "'Not accepted'.concat($[inEvent].json[contextData].[equipment].[instances])"
  }
}
},

I know my nodes are not right but I am a bit lost and don’t even know if it is possible that why I added some count ??? in the policy node.

I also added 4 different examples of calculations I would need to make.

I will try to explain it a bit better.

I want just like you make it with the criteria’s topic.id and location.id is matched in the IF node and that is not the challenge. The challenge comes with the 3rd criteria that is stored in the policy node and which is based on a formula

Here is the example

In exmaple 1 shall the If formula be true if $inEvent.topic.id = $policy.topic.id
and $inEvent.location.id = $policy.location.id then i would like a 3rd cirteria based
on a fomrula in in $policy.policy that contains a formula that calculates
if $inEvenet.contextdata.equipment.id = “a statement stored in the policy node”
and that calculates if there are $inData.contextData.equipment.instances where stete=Active
is 2 or more.

{
"topic":{
"id": "exampleEvent1"
 },
"locationdata":{
  "id": "Location1"
},
"contextData":{
  "equipment": 
  {
    "id": "equipment1",
    "instances":[
    {
      "id": "i1",
      "state": "active"
    },
    {
      "id": "i2",
      "state": "inactive"
    },
    {
      "id": "i3",
      "state": "active"
    }
    ]
  }

  
}
}

In exmaple 2 shall the If formula be true if $inEvent.topic.id = $policy.topic.id
and $inEvent.location.id = $policy.location.id then i would like a 3rd cirteria based
on a fomrula in in $policy.policy that contains a formula that calculates
if $inEvenet.contextdata.enviroment.wind.speed > 10

{
"topic":{
"id": "exampleEvent2"
},
"locationdata":{
  "id": "Location1"
},
"contextData":{
  "enviroment": 
  {
    "wind":{
      "speed": "10",
      "format": "m/s"
    }
  }
}
},

Exmaple 3 is the same as exmaple 2 i just wanted to have another property of
current.speed instead of wind.speed

{
"topic":{
"id": "exampleEvent3"
},
"contextData":{
  "enviroment": 
  {
    "current":{
      "speed": "10",
      "format": "m/s"
    }
  }
}
},

In exmaple 4 shall the If formula be true if $inEvent.topic.id = $policy.topic.id
and $inEvent.location.id = $policy.location.id then i would like a 3rd cirteria based
on a fomrula in in $policy.policy that contains a formula that calculates
if $inEvenet.contextdata.equipment.id = “a statement stored in the policy node”
and that calculates if there are $inData.contextData.equipment.instances

id = i1
state=Active

and

id = i2
State = Active

and

id = i2
State = inActive

{
"topic":{
"id": "exampleEvent4"
},
"locationdata":{
  "id": "Location1"
},
"contextData":{
  "equipment": 
  {
    "id": "equipment1",
    "instances":[
    {
      "id": "i1",
      "state": "active"
    },
    {
      "id": "i2",
      "state": "active"
    },
    {
      "id": "i3",
      "state": "inactive"
    }
    ]
  }
}
}

The idea was to have a formula in the property policy that calculated if it was true or false and then add it as 3rd criteria in if formula. However I struggle with formulas that gets stored

Hi

I managed to solve it by a workaround. The thing i wanted to achieve was to store to calculation of true or false in the policy json so based on the convert rule it should return true of false.

But i solved it with this workaround.

  "nodes": [
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{$node[\"inEvent\"].json[\"topic\"][\"id\"]}}",
              "value2": "={{$node[\"policy\"].json[\"topic\"][\"id\"]}}"
            },
            {
              "value1": "={{$node[\"inEvent\"].json[\"locationdata\"][\"id\"]}}",
              "value2": "={{$node[\"policy\"].json[\"locationdata\"][\"id\"]}}"
            }
          ],
          "number": [
            {
              "value1": "={{$node[\"Function\"].json[\"activeValue\"]}}",
              "operation": "={{$node[\"Convert\"].json[\"validationdata\"][\"policy\"][\"rule\"]}}",
              "value2": "={{$node[\"Convert\"].json[\"validationdata\"][\"policy\"][\"policyValue\"]}}"
            }
          ]
        }
      },
      "name": "IF2",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        1680,
        340
      ]
    },
    {
      "parameters": {
        "functionCode": "var policyValues = items[0].json.validationdata.policy.setValue;\n\nvar activePolicyStates =policyValues.filter(value => value.state === \"active\");\n\nreturn [{json:{\"activeValue\": activePolicyStates.length}}];\n\n\n"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        1530,
        340
      ]
    }
  ],
  "connections": {
    "Function": {
      "main": [
        [
          {
            "node": "IF2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}