How to simplify boolean expression

Hello there, is there a way to simplify this boolean expression?
When I try $boolean(payload.errorCode=200) it always return false. I think because it is empty. And in the future i can have 5 or 10 elements, is it a way to do like payload(x).errorCode so I can do it even with multiple elements? Thanks in advance for your help.

Hi @kjbambe.
Welcome to the forum.

I think that is failing because payload(errorCode) isn't exactly a TRUE/FALSE thing.

What are you trying to do?

Hi @Trying_to_learn thanks for the feedback. I corrected the inquiry description. It is $boolean(payload.errorCode=200). And with this it is still always false.

So you want a TRUE/FALSE if the payload.error == 200?

Ah, I think that's a problem.

Try == and not just =

= is different to ==.
= sets the payload.errorCode to 200.
== will compare it.

I think.

What I am trying to do is if all the payload.errorCode = 200. the boolean expression will return TRUE.

I will try! Thanks :wink:

No problems.

Don't be afraid to make mistakes. I know from experience that there are a lot of them to learn.
It is sometimes easier to learn from other's mistakes. :wink:

Oh, and don't post too many times at first. You will run out of free posts before you know and then will be stuck until your account is given full posting permission.

1 Like

I tried $boolean(payload.errorCode==200) but i got this error:

Invalid JSONata expression:
The symbol "=" cannot be used as a unary operator

Is there something I am missing?

No, probably me.

Before you run out of posts:
What is it you are trying to do?
Why are you doing it this way?

The catch node is usually the one to catch errors.
Then parsing the errors would be easier.
Use a switch node to send the message to a desired output/path.

Write in English what it is you want to do.
Then break that into bits/parts that do things.
Then write down what each part does.

Have you every used flow charts? It could help you.
But writing it down (and posting it here) someone may help you with how to do it.

How many posts do I have?

What I am trying to do is to simplify this expression
$boolean(payload[0].errorCode=200 and payload[1].errorCode=200 and payload[2].errorCode=200)
that will work even though i have upto payload[10]. If all the payload.errorCode = 200. the boolean expression will return TRUE.

Why are you wanting to use boolean?
That could be causing you grief you don't need.

why not do something like:

let x = 0;
x = payload[].errorCode;
if (x == 200)
{
  do what you want done here.
}

This would look at all payload[].errorCode values because the [] is empty and so would/should look at them all.

Note the word LIKE.

I am also having a bad time trying to read the screen shot.
I fear the structure of the payload isn't helping and could be wrong.
(Which wouldn't be helping you do what you want to do.)

Thanks for that. Unfortunately I am not familiar yet with function node.
I am trying to use a switch node for this.
And the payload looks like this, I am assured this is in good structure.

So if ANY of the errorCode values are set to 200 you want to be told.... Yes?

1 Like

Exactly :slightly_smiling_face:

Do you know how to import a flow (code) if I post it?

Here is an EXAMPLE of what I understand you want to do.

[{"id":"b167edbde82cac1a","type":"function","z":"0918ee609bf69fc7","name":"function 15","func":"msg.payload = [\n    {\n        \"errorCode\":200,\n        \"desciption\":\"Something\"\n    },\n    {\n        \"errorCode\":0,\n        \"description\":\"another one\"\n    }\n]\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":4170,"y":3440,"wires":[["87f38d046e2aa0a9"]]},{"id":"b658fcffa8d8426c","type":"inject","z":"0918ee609bf69fc7","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"foo","payloadType":"str","x":4010,"y":3440,"wires":[["b167edbde82cac1a"]]},{"id":"87f38d046e2aa0a9","type":"function","z":"0918ee609bf69fc7","name":"function 16","func":"//  Repeat this 10 times.\n//  You may need to start at `a` though rather than `x`\n//  But I hope you get the idea.\nlet x = msg.payload[0].errorCode;\n//node.warn(\"X \" + x);\nlet y = msg.payload[1].errorCode;\n//node.warn(\"Y \" + y);\n\nif ((x || y) == 200)\n{\n    node.warn(\"We have detected a problem\");\n    //  You need to put your code here.\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":4370,"y":3440,"wires":[[]]},{"id":"b66f671e6239dbb5","type":"function","z":"0918ee609bf69fc7","name":"function 17","func":"msg.payload = [\n    {\n        \"errorCode\":0,\n        \"desciption\":\"Something\"\n    },\n    {\n        \"errorCode\":200,\n        \"description\":\"another one\"\n    }\n]\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":4170,"y":3480,"wires":[["87f38d046e2aa0a9"]]},{"id":"299174c00efcca15","type":"function","z":"0918ee609bf69fc7","name":"function 18","func":"msg.payload = [\n    {\n        \"errorCode\":0,\n        \"desciption\":\"Something\"\n    },\n    {\n        \"errorCode\":0,\n        \"description\":\"another one\"\n    }\n]\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":4170,"y":3400,"wires":[["87f38d046e2aa0a9"]]},{"id":"f8a830081d30cfb3","type":"inject","z":"0918ee609bf69fc7","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"foo","payloadType":"str","x":4010,"y":3480,"wires":[["b66f671e6239dbb5"]]},{"id":"55ba2228ab69b05d","type":"inject","z":"0918ee609bf69fc7","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"foo","payloadType":"str","x":4010,"y":3400,"wires":[["299174c00efcca15"]]}]

Hover over the code (above) and move to the right.
Look for the clipboard icon to appear.
Click it and it copies it to your clipboard.
Then you IMPORT it to your machine.

Top inject node. All good.
Middle inject node. 0 is/has a problem.
Bottom inject node. 1 is/has a problem.

If you want to know if any errorCodes are 200
try

[{"id":"989e880ce2cf99af","type":"inject","z":"452103ea51141731","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"errorCode\":200},{\"errorCode\":200},{\"errorCode\":200}]","payloadType":"json","x":110,"y":4720,"wires":[["b3865f0325505050"]]},{"id":"b3865f0325505050","type":"switch","z":"452103ea51141731","name":"","property":"200 in $$.payload.errorCode","propertyType":"jsonata","rules":[{"t":"true"},{"t":"false"}],"checkall":"true","repair":false,"outputs":2,"x":310,"y":4720,"wires":[["79a6e7d13e9387bf"],["49f51f73c5535fb5"]]},{"id":"79a6e7d13e9387bf","type":"debug","z":"452103ea51141731","name":"a error code is 200","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":630,"y":4700,"wires":[]},{"id":"49f51f73c5535fb5","type":"debug","z":"452103ea51141731","name":"no 200 error codes","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":630,"y":4740,"wires":[]}]

How to import a flow

expression

200 in $$.payload.errorCode

Or if you want to make sure all errorCode's are 200

$not(false in $$.payload.errorCode.($=200))
1 Like

Sorry i do not know yet how to import the flow. But thanks for this. I will try when I can import.

Thanks for this! this expression is what solved my issue :hugs:

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