Edit: corrected install directory
I have been struggling to get data from Linux commands into Node-red as JSON format and stumbled upon this utility.
Maybe other people will find it useful.
jc is a Python based command line utility which understands the output of many Linux commands and formats it as json (or do I mean javascript string?)
To install it sudo pip3 install jc. It’s installed to /usr/local/bin
More info at https://blog.kellybrazil.com/2019/11/26/bringing-the-unix-philosophy-to-the-21st-century/
and https://github.com/kellyjonbrazil/jc
Three ways to get Linux CLI data into Node-red:
-
You can use the exec node to run a command
free -m | grep -v total
This returns a string which you have to parse -
Use a shell script to reformat the output as a json string eg using awk
$ cat myawkwrapper.sh
#! /bin/bash
mem=$(free -m | grep -v total | awk '
{printf "{\"type\": \"%s\", \"total\": %d, \"used\": %d, \"free\": %d, \"available\": %d },\n",
$1, $2, $3, $4, $7 }' )
mem=[${mem:0:-1}]
echo $mem
This returns a string which the json node accepts and converts into the object
[
{"type":"Mem:","total":1871,"used":223,"free":1082,"available":1539},
{"type":"Swap:","total":1871,"used":0,"free":1871,"available":0}
]
- A much easier option (for commands it supports): jc
free -m | jc –free
This returns a very similar string which converts to
[
{"type":"Mem","total":1871,"used":221,"free":1084,"shared":24,"buff_cache":565,"available":1541},
{"type":"Swap","total":1871,"used":0,"free":1871}
]
[{"id":"81232669e7d1ad84","type":"tab","label":"Flow 6","disabled":false,"info":"","env":[]},{"id":"4e1464b54c2160e1","type":"inject","z":"81232669e7d1ad84","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"free -m","payloadType":"str","x":390,"y":160,"wires":[["e48fe23e797d5aa1"]]},{"id":"d417f6bdaeab1aaf","type":"inject","z":"81232669e7d1ad84","name":"My awk wrapper","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"/home/pi/myawkwrapper.sh","payloadType":"str","x":360,"y":240,"wires":[["8a32405c33195801"]]},{"id":"8a32405c33195801","type":"exec","z":"81232669e7d1ad84","command":"","addpay":"payload","append":"","useSpawn":"false","timer":"","winHide":false,"oldrc":false,"name":"","x":530,"y":240,"wires":[["bffaa6b12ad23ade"],[],[]]},{"id":"bffaa6b12ad23ade","type":"json","z":"81232669e7d1ad84","name":"Convert to js object","property":"payload","action":"obj","pretty":false,"x":710,"y":240,"wires":[["484d562f92e3e176"]]},{"id":"484d562f92e3e176","type":"debug","z":"81232669e7d1ad84","name":"Version 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":900,"y":240,"wires":[]},{"id":"e48fe23e797d5aa1","type":"exec","z":"81232669e7d1ad84","command":"","addpay":"payload","append":"","useSpawn":"false","timer":"","winHide":false,"oldrc":false,"name":"","x":530,"y":160,"wires":[["8d6406fb26c4dc7e"],[],[]]},{"id":"8d6406fb26c4dc7e","type":"debug","z":"81232669e7d1ad84","name":"Version 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":900,"y":160,"wires":[]},{"id":"4825cb8dcd1cf607","type":"inject","z":"81232669e7d1ad84","name":"free -m | jc --free","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"free -m | jc --free","payloadType":"str","x":360,"y":320,"wires":[["ba24cafbc46e1c65"]]},{"id":"ba24cafbc46e1c65","type":"exec","z":"81232669e7d1ad84","command":"","addpay":"payload","append":"","useSpawn":"false","timer":"","winHide":false,"oldrc":false,"name":"","x":530,"y":320,"wires":[["b2f1e4ba696a2a3a"],[],[]]},{"id":"b2f1e4ba696a2a3a","type":"json","z":"81232669e7d1ad84","name":"Convert to js object","property":"payload","action":"obj","pretty":false,"x":710,"y":320,"wires":[["bdf50e199fd7a19c"]]},{"id":"bdf50e199fd7a19c","type":"debug","z":"81232669e7d1ad84","name":"Version 3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":900,"y":320,"wires":[]},{"id":"c7a5e8a8c7a4a7c2","type":"comment","z":"81232669e7d1ad84","name":"Exec free -m directly","info":"","x":350,"y":120,"wires":[]},{"id":"59fdd6f5e291d1ea","type":"comment","z":"81232669e7d1ad84","name":"Shell script to reformat it","info":"","x":330,"y":200,"wires":[]},{"id":"929e31bbf52e5f73","type":"comment","z":"81232669e7d1ad84","name":"Reformat it with jc","info":"","x":350,"y":280,"wires":[]}]