# How to simplify boolean expression

**URL:** <https://discourse.nodered.org/t/how-to-simplify-boolean-expression/75670>\
**Category:** General\
**Created:** [24 February 2023 07:18 UTC](https://discourse.nodered.org/t/how-to-simplify-boolean-expression/75670 "2023-02-24T07:18:21Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![kjbambe](https://avatars.discourse-cdn.com/v4/letter/k/ea666f/32.png) [@kjbambe](https://discourse.nodered.org/u/kjbambe)\
**Post date:** [24 February 2023 07:18 UTC](https://discourse.nodered.org/t/how-to-simplify-boolean-expression/75670/1 "2023-02-24T07:18:21Z")

</div>

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.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/c/1ce1a7b1ab72c3461a0310eb50b8cce4ec49d903.jpeg)

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [24 February 2023 07:23 UTC](https://discourse.nodered.org/t/how-to-simplify-boolean-expression/75670/2 "2023-02-24T07:23:01Z")

</div>

Hi @kjbambe.  
Welcome to the forum.

> [@kjbambe](#):
>
> $boolean(payload.(errorCode)) it always return false

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

What are you trying to do?

---

<div class="post-metadata">

**Author:** ![kjbambe](https://avatars.discourse-cdn.com/v4/letter/k/ea666f/32.png) [@kjbambe](https://discourse.nodered.org/u/kjbambe)\
**Post date:** [24 February 2023 07:31 UTC](https://discourse.nodered.org/t/how-to-simplify-boolean-expression/75670/3 "2023-02-24T07:31:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [24 February 2023 07:34 UTC](https://discourse.nodered.org/t/how-to-simplify-boolean-expression/75670/4 "2023-02-24T07:34:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kjbambe](https://avatars.discourse-cdn.com/v4/letter/k/ea666f/32.png) [@kjbambe](https://discourse.nodered.org/u/kjbambe)\
**Post date:** [24 February 2023 07:35 UTC](https://discourse.nodered.org/t/how-to-simplify-boolean-expression/75670/5 "2023-02-24T07:35:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kjbambe](https://avatars.discourse-cdn.com/v4/letter/k/ea666f/32.png) [@kjbambe](https://discourse.nodered.org/u/kjbambe)\
**Post date:** [24 February 2023 07:35 UTC](https://discourse.nodered.org/t/how-to-simplify-boolean-expression/75670/6 "2023-02-24T07:35:56Z")

</div>

I will try! Thanks 😉

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [24 February 2023 07:37 UTC](https://discourse.nodered.org/t/how-to-simplify-boolean-expression/75670/7 "2023-02-24T07:37:22Z")

</div>

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. 😉

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.

---

<div class="post-metadata">

**Author:** ![kjbambe](https://avatars.discourse-cdn.com/v4/letter/k/ea666f/32.png) [@kjbambe](https://discourse.nodered.org/u/kjbambe)\
**Post date:** [24 February 2023 07:40 UTC](https://discourse.nodered.org/t/how-to-simplify-boolean-expression/75670/8 "2023-02-24T07:40:22Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [24 February 2023 07:42 UTC](https://discourse.nodered.org/t/how-to-simplify-boolean-expression/75670/9 "2023-02-24T07:42:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kjbambe](https://avatars.discourse-cdn.com/v4/letter/k/ea666f/32.png) [@kjbambe](https://discourse.nodered.org/u/kjbambe)\
**Post date:** [24 February 2023 07:47 UTC](https://discourse.nodered.org/t/how-to-simplify-boolean-expression/75670/10 "2023-02-24T07:47:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [24 February 2023 07:51 UTC](https://discourse.nodered.org/t/how-to-simplify-boolean-expression/75670/11 "2023-02-24T07:51:43Z")

</div>

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

why not do something like:

```auto
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.)

---

<div class="post-metadata">

**Author:** ![kjbambe](https://avatars.discourse-cdn.com/v4/letter/k/ea666f/32.png) [@kjbambe](https://discourse.nodered.org/u/kjbambe)\
**Post date:** [24 February 2023 08:10 UTC](https://discourse.nodered.org/t/how-to-simplify-boolean-expression/75670/12 "2023-02-24T08:10:17Z")

</div>

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.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/a/ba03e79a154daa1a0c6abab01c66ba8150f7ee09.png)

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [24 February 2023 08:12 UTC](https://discourse.nodered.org/t/how-to-simplify-boolean-expression/75670/13 "2023-02-24T08:12:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kjbambe](https://avatars.discourse-cdn.com/v4/letter/k/ea666f/32.png) [@kjbambe](https://discourse.nodered.org/u/kjbambe)\
**Post date:** [24 February 2023 08:14 UTC](https://discourse.nodered.org/t/how-to-simplify-boolean-expression/75670/14 "2023-02-24T08:14:53Z")

</div>

Exactly 🙂

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [24 February 2023 08:17 UTC](https://discourse.nodered.org/t/how-to-simplify-boolean-expression/75670/15 "2023-02-24T08:17:00Z")

</div>

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.

```auto
[{"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.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [24 February 2023 08:53 UTC](https://discourse.nodered.org/t/how-to-simplify-boolean-expression/75670/16 "2023-02-24T08:53:35Z")

</div>

If you want to know if any errorCodes are 200  
try

```auto
[{"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](https://nodered.org/docs/user-guide/editor/workspace/import-export)

expression

```auto
200 in $$.payload.errorCode

```

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

```auto
$not(false in $$.payload.errorCode.($=200))

```

---

<div class="post-metadata">

**Author:** ![kjbambe](https://avatars.discourse-cdn.com/v4/letter/k/ea666f/32.png) [@kjbambe](https://discourse.nodered.org/u/kjbambe)\
**Post date:** [24 February 2023 11:47 UTC](https://discourse.nodered.org/t/how-to-simplify-boolean-expression/75670/17 "2023-02-24T11:47:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kjbambe](https://avatars.discourse-cdn.com/v4/letter/k/ea666f/32.png) [@kjbambe](https://discourse.nodered.org/u/kjbambe)\
**Post date:** [24 February 2023 11:56 UTC](https://discourse.nodered.org/t/how-to-simplify-boolean-expression/75670/18 "2023-02-24T11:56:35Z")

</div>

> [@E1cid](#):
>
> `$not(false in $.payload.errorCode.($=200))`

Thanks for this! this expression is what solved my issue 🤗

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [10 March 2023 11:57 UTC](https://discourse.nodered.org/t/how-to-simplify-boolean-expression/75670/19 "2023-03-10T11:57:24Z")

</div>

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