How to remove empty value in array using change node

I have a text file that contains an empty line like this:


a

b
c

d

e


f

When reading the file I want the resulting array to get rid of the empty values using the $filter of the change node.
https://docs.jsonata.org/higher-order-functions#filter
..but not working, i don't know what went wrong?
Please tell me. Thanks!

node-change-filter

[{"id":"8d9ec817.efd0b8","type":"inject","z":"f1e299b0.7d26b8","name":"","topic":"","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":230,"y":1600,"wires":[["8926455e.3b12c8"]]},{"id":"8926455e.3b12c8","type":"file in","z":"f1e299b0.7d26b8","name":"","filename":"/Temp/File1.txt","format":"utf8","chunk":false,"sendError":false,"encoding":"none","x":380,"y":1600,"wires":[["b5a982d9.951d2"]]},{"id":"1fe3fff6.16cab","type":"debug","z":"f1e299b0.7d26b8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":870,"y":1600,"wires":[]},{"id":"b5a982d9.951d2","type":"change","z":"f1e299b0.7d26b8","name":"$split(msg.payload,'\\n')","rules":[{"t":"set","p":"srcArray","pt":"msg","to":"$split(msg.payload,'\\n')","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":580,"y":1600,"wires":[["c2c03026.b1631"]]},{"id":"c2c03026.b1631","type":"change","z":"f1e299b0.7d26b8","name":"$filter","rules":[{"t":"set","p":"destArray","pt":"msg","to":"$filter(srcArray, function($v) {\t  0 < $length($v)\t})","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":750,"y":1600,"wires":[["1fe3fff6.16cab"]]}]

In trying your code, I copied the file content you included. I noticed that the 'lines' actually appear to be tabs. When I changed the "\n" to "\t", the code worked as expected. So maybe your file actually has tabs in it, not newlines.

If you want to handle newlines or tabs, you could use a regular expression instead:

$split(payload, /[\n\t]/)
1 Like

I use Windows, and in the file content is newlines, not tabs...

node-change-filter-1

I tried $split(payload, /[\n\t]/) but still doesn't work :frowning:

Hm, it works for me, but I'm on Ubuntu. Given you're on Windows, they are probably carriage returns, so try "\r". Although Windows normally has line ends as "\n\r".

I also tried "\n\r", "/(\r\n|\n|\r)/" but still doesn't work.
I'm trying with $replace

Could we see the output of the debug message after you split the file text? Not a screenshot

1 Like

If I read the following :

then for PC (windows) the regex to match newline characters should be \r\n instead of \n