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

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

ini-check-value

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

[{"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!

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.

1 Like

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

[{"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":[]}]
1 Like

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

1 Like

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.

1 Like

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

1 Like

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.

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.

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.

1 Like

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-

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

1 Like

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

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

Here is another solution you can add any missing =

[{"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"]]}]
1 Like

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.

[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!

I'll have tea and offer a solution.

2 Likes

You are so funny :smile:

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.

1 Like

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

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

[QUESTION] <title> #98

1 Like

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

[{"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"]]}]
1 Like

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