# How to check if the key's value exists or not

**URL:** <https://discourse.nodered.org/t/how-to-check-if-the-keys-value-exists-or-not/34167>\
**Category:** General\
**Created:** [13 October 2020 08:46 UTC](https://discourse.nodered.org/t/how-to-check-if-the-keys-value-exists-or-not/34167 "2020-10-13T08:46:29Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![minhhn0205](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/minhhn0205/32/29062_2.png) [@minhhn0205](https://discourse.nodered.org/u/minhhn0205)\
**Post date:** [13 October 2020 08:46 UTC](https://discourse.nodered.org/t/how-to-check-if-the-keys-value-exists-or-not/34167/1 "2020-10-13T08:46:29Z")

</div>

Hi,  
I have a `config.ini` file with values of `Key2` and `Key3` not yet set:

![ini-check-value](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/6/d61128cb9832e7882222a94312d1f3297658fe98.png)

When using `ini` node to get the contents of file `config.ini`, the value of **Key2** is **true** even though it is not set

![ini-check-value-2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/a/0a16b7b543342ef83e2c041e6dd7a30e381492e7.png)

```auto
[{"id":"66a40383.a27bdc","type":"parser-ini","z":"f1e299b0.7d26b8","property":"payload","name":"","x":1850,"y":3040,"wires":[["aacd1328.bf48e"]]},{"id":"88685d9.39f9aa","type":"file in","z":"f1e299b0.7d26b8","name":"","filename":"/Temp/config.ini","format":"utf8","chunk":false,"sendError":false,"encoding":"utf8","x":2000,"y":2980,"wires":[["66a40383.a27bdc"]]},{"id":"66807c11.df1a34","type":"inject","z":"f1e299b0.7d26b8","name":"","topic":"","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":1830,"y":2980,"wires":[["88685d9.39f9aa"]]},{"id":"aacd1328.bf48e","type":"debug","z":"f1e299b0.7d26b8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":2010,"y":3040,"wires":[]},{"id":"119aa9ee.339fb6","type":"function","z":"f1e299b0.7d26b8","name":"function","func":"\nif (0 === Object.values(msg.payload['Session']['Key2']).length) {\n //msg.valueKey2 = 'value not found';\n msg.valueKey2 = msg.payload['Session']['Key2'];\n}\n\nif (0 === Object.values(msg.payload['Session']['Key4']).length) {\n msg.valueKey4 = msg.payload['Session']['Key4'];\n}\n\nreturn msg;","outputs":1,"noerr":0,"x":1960,"y":3100,"wires":[["aacd1328.bf48e"]]}]

```

I do not want to use `function` node, can I check with any other core node, such as `change` node?  
Please tell me.  
Thanks!

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [13 October 2020 09:17 UTC](https://discourse.nodered.org/t/how-to-check-if-the-keys-value-exists-or-not/34167/2 "2020-10-13T09:17:25Z")

</div>

What exactly do you want to check for?

You can use the Switch node to test the value of a property, but I'm not sure that is what you mean.

If you want to know that `Key2` had no value set in the ini file, there is no way you can do that using the output of the `ini` node. It looks like the `ini` node is setting the value to `true` - which is identical to the value given to `Key4`.

If you want to know that `Key2` has no value set in the file, you'll need to parse the file yourself, or contact the author of the `ini` node to see if they can add an option to the node to do it for you.

---

<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:** [13 October 2020 09:20 UTC](https://discourse.nodered.org/t/how-to-check-if-the-keys-value-exists-or-not/34167/3 "2020-10-13T09:20:05Z")

</div>

You could use change to evaluate the object and return any "" as false.

```auto
[{"id":"dd3930f9.05cc6","type":"change","z":"7760f563.ea0324","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$each(\t payload,\t function($v, $i) {$v = \"\" ? {$i: false} : {$i: $v}}\t)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":510,"y":1320,"wires":[["d6d6a27b.5b5538"]]},{"id":"3c8ce754.fda3e","type":"inject","z":"7760f563.ea0324","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"1\": true, \"2\": \"\", \"3\": false}","payloadType":"json","x":320,"y":1320,"wires":[["dd3930f9.05cc6"]]},{"id":"d6d6a27b.5b5538","type":"debug","z":"7760f563.ea0324","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":710,"y":1320,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [13 October 2020 09:22 UTC](https://discourse.nodered.org/t/how-to-check-if-the-keys-value-exists-or-not/34167/4 "2020-10-13T09:22:00Z")

</div>

@E1cid that assumes that `""` isn't a valid value for a property to have. It also ignores the case of `Key2` which has no value, but the ini node has given it a value of `true`.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [13 October 2020 09:22 UTC](https://discourse.nodered.org/t/how-to-check-if-the-keys-value-exists-or-not/34167/5 "2020-10-13T09:22:40Z")

</div>

Is that a valid ini file, with no = after Key2? If a particular application considers that to be valid I guess you would need to determine what it considers the name without the = to mean.

---

<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:** [13 October 2020 09:24 UTC](https://discourse.nodered.org/t/how-to-check-if-the-keys-value-exists-or-not/34167/6 "2020-10-13T09:24:34Z")

</div>

Yes, but the Op is the only one who knows that, he wanted to know if he could check with any other core node. I gave an example of that, it did not cover all cases

---

<div class="post-metadata">

**Author:** ![minhhn0205](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/minhhn0205/32/29062_2.png) [@minhhn0205](https://discourse.nodered.org/u/minhhn0205)\
**Post date:** [13 October 2020 14:35 UTC](https://discourse.nodered.org/t/how-to-check-if-the-keys-value-exists-or-not/34167/7 "2020-10-13T14:35:22Z")

</div>

> [@knolleary](#):
>
> What exactly do you want to check for?

> [@Colin](#):
>
> Is that a valid ini file, with no = after Key2?

I'm trying to validate the ini file, if the **session** , **key** , and **value** information is invalid it will output an error and stop the node-red application.

> [@E1cid](#):
>
> You could use change to evaluate the object and return any "" as false.

Thank you for your effort!  
In case `Key3=`, the result given is empty, I can judge that the value of **Key3** is empty,  
but case with no = after `Key2` the ini node has given it a value of `true`,  
so I cannot judge whether the value that was set from the beginning is `true` like **Key4** or if the input error has no = but the given value is `true`.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [13 October 2020 14:59 UTC](https://discourse.nodered.org/t/how-to-check-if-the-keys-value-exists-or-not/34167/8 "2020-10-13T14:59:22Z")

</div>

> [@minhhn0205](#):
>
> I'm trying to validate the ini file

That makes sense. This sounds like a question for the node author. The node's page on the node red flows site should have a link to its github repository. You can raise an issue there.

---

<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:** [13 October 2020 15:48 UTC](https://discourse.nodered.org/t/how-to-check-if-the-keys-value-exists-or-not/34167/9 "2020-10-13T15:48:03Z")

</div>

Then you would need to check the ini file before processing, or proccess it your self.

If your file has that format then you could try something like-

```auto
[{"id":"d5cc6037.6bc238","type":"change","z":"7760f563.ea0324","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$count(\t $filter(\t $split(payload, \"\\n\"),\t function($v){$count($split($v,\"=\")) <= 1}\t)\t) < 2 ? payload : false ","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":720,"y":1640,"wires":[["d6d6a27b.5b5538"]]},{"id":"e56da466.e65728","type":"inject","z":"7760f563.ea0324","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[\"0x1\",\"0x67\",\"0x33\",\"0x1\",\"0x2\",\"0x68\",\"0x6e\"]","payloadType":"json","x":310,"y":1640,"wires":[["4e82f442.bde2e4"]]},{"id":"4e82f442.bde2e4","type":"template","z":"7760f563.ea0324","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"[Session]\nkey1=value1\nkey2\nkey3=\nkey4=value4[EOF]","output":"str","x":470,"y":1640,"wires":[["d5cc6037.6bc238"]]},{"id":"d6d6a27b.5b5538","type":"debug","z":"7760f563.ea0324","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":710,"y":1320,"wires":[]}]

```

this will return false if more that 2 lines dont have an equal sign. And return payload if all lines have equal sign, except top line.

---

<div class="post-metadata">

**Author:** ![minhhn0205](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/minhhn0205/32/29062_2.png) [@minhhn0205](https://discourse.nodered.org/u/minhhn0205)\
**Post date:** [13 October 2020 16:30 UTC](https://discourse.nodered.org/t/how-to-check-if-the-keys-value-exists-or-not/34167/10 "2020-10-13T16:30:07Z")

</div>

> [@Colin](#):
>
> The node's page on the node red flows site should have a link to its github repository. You can raise an issue there.

I raised an issue on the repository.  
[https://github.com/npm/ini/issues/98](https://github.com/npm/ini/issues/98)

---

<div class="post-metadata">

**Author:** ![minhhn0205](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/minhhn0205/32/29062_2.png) [@minhhn0205](https://discourse.nodered.org/u/minhhn0205)\
**Post date:** [13 October 2020 16:31 UTC](https://discourse.nodered.org/t/how-to-check-if-the-keys-value-exists-or-not/34167/11 "2020-10-13T16:31:29Z")

</div>

> [@E1cid](#):
>
> return false if more that 2 lines dont have an equal sign. And return payload if all lines have equal sign, except top line.

This is a great idea. Thank you very much!!!

---

<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:** [13 October 2020 16:35 UTC](https://discourse.nodered.org/t/how-to-check-if-the-keys-value-exists-or-not/34167/12 "2020-10-13T16:35:16Z")

</div>

Here is another solution you can add any missing =

```auto
[{"id":"e56da466.e65728","type":"inject","z":"7760f563.ea0324","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[\"0x1\",\"0x67\",\"0x33\",\"0x1\",\"0x2\",\"0x68\",\"0x6e\"]","payloadType":"json","x":310,"y":1640,"wires":[["4e82f442.bde2e4"]]},{"id":"4e82f442.bde2e4","type":"template","z":"7760f563.ea0324","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"[Session]\nkey1=value1\nkey2\nkey3=\nkey4=value4[EOF]","output":"str","x":470,"y":1640,"wires":[["d5cc6037.6bc238"]]},{"id":"d5cc6037.6bc238","type":"change","z":"7760f563.ea0324","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$join(\t $map(\t $split(payload, \"\\n\"),\t function($v, $i){\t $count($split($v,\"=\")) > 1 or $i = 0 ? $v : $v & '='\t }\t ),\t \"\\n\"\t)\t\t\t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":720,"y":1640,"wires":[["d6d6a27b.5b5538"]]}]

```

---

<div class="post-metadata">

**Author:** ![minhhn0205](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/minhhn0205/32/29062_2.png) [@minhhn0205](https://discourse.nodered.org/u/minhhn0205)\
**Post date:** [13 October 2020 17:00 UTC](https://discourse.nodered.org/t/how-to-check-if-the-keys-value-exists-or-not/34167/13 "2020-10-13T17:00:55Z")

</div>

I think your 2nd solution is better than the one above!  
but I just tried to confirm with an ini file that has _many sessions_ and _there are comments per key_, both solutions are not working well.

```auto
[Session1]
#comment 1
key1=value1
#comment 2
key2
#comment 3
key3=
#comment 4
key4=value4

[Session2]
key1=value1
key2
key3=
key4=value4[EOF]

```

Because in the absence of an equal sign after the key is a limitation of the `ini` node, so I think I will use the `function` node in combination with the javascript code to validate the content of the ini file.

Thank you so much for your assistance!

---

<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:** [13 October 2020 17:11 UTC](https://discourse.nodered.org/t/how-to-check-if-the-keys-value-exists-or-not/34167/14 "2020-10-13T17:11:24Z")

</div>

I'll have tea and offer a solution.

---

<div class="post-metadata">

**Author:** ![minhhn0205](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/minhhn0205/32/29062_2.png) [@minhhn0205](https://discourse.nodered.org/u/minhhn0205)\
**Post date:** [13 October 2020 17:12 UTC](https://discourse.nodered.org/t/how-to-check-if-the-keys-value-exists-or-not/34167/15 "2020-10-13T17:12:33Z")

</div>

You are so funny 😄

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [13 October 2020 17:32 UTC](https://discourse.nodered.org/t/how-to-check-if-the-keys-value-exists-or-not/34167/16 "2020-10-13T17:32:19Z")

</div>

You may want to edit the issue you've raised to give it a proper title.

And don't forget, youve raised it against the underlying npm module that is unlikely to know anything about Node-RED.

---

<div class="post-metadata">

**Author:** ![minhhn0205](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/minhhn0205/32/29062_2.png) [@minhhn0205](https://discourse.nodered.org/u/minhhn0205)\
**Post date:** [13 October 2020 17:36 UTC](https://discourse.nodered.org/t/how-to-check-if-the-keys-value-exists-or-not/34167/17 "2020-10-13T17:36:56Z")

</div>

> [@knolleary](#):
>
> You may want to edit the issue you've raised to give it a proper title.

You mean I should edit the title of this topic, right?

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [13 October 2020 17:39 UTC](https://discourse.nodered.org/t/how-to-check-if-the-keys-value-exists-or-not/34167/18 "2020-10-13T17:39:28Z")

</div>

No, I mean the GitHub issue you have raised. At the moment its title is:

> ` [QUESTION] <title> #98`

---

<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:** [13 October 2020 17:49 UTC](https://discourse.nodered.org/t/how-to-check-if-the-keys-value-exists-or-not/34167/19 "2020-10-13T17:49:02Z")

</div>

Ok feeling better try this, should handle emtpy line, #Comment, [Session] .

```auto
[{"id":"47d5a245.2d2b3c","type":"change","z":"7760f563.ea0324","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$join(\t $map(\t $split(payload, \"\\n\"),\t function($v){\t $contains($substring($trim($v), 0, 1),/#|\\[/) or \t $count($split($v,\"=\")) > 1 or \t $trim($v) = \"\" \t ? $v : $v & '='\t }\t ),\t \"\\n\"\t)\t\t\t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":700,"y":1720,"wires":[["d6d6a27b.5b5538","7f27e2e4.a94a1c"]]}]

```

---

<div class="post-metadata">

**Author:** ![minhhn0205](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/minhhn0205/32/29062_2.png) [@minhhn0205](https://discourse.nodered.org/u/minhhn0205)\
**Post date:** [13 October 2020 17:58 UTC](https://discourse.nodered.org/t/how-to-check-if-the-keys-value-exists-or-not/34167/20 "2020-10-13T17:58:59Z")

</div>

> [@knolleary](#):
>
> No, I mean the GitHub issue you have raised.

Thank you for alert,  
earlier I focused on writing content but forgot to write the title.

[Next page](https://discourse.nodered.org/t/how-to-check-if-the-keys-value-exists-or-not/34167.md?page=2)
