I want to create bash script that it converts output of other scripts (Normally simple text) in a valid json form. How can I do this ?
Example:
awk '/^Mem/ {printf("%u%%", 100*$3/$2);}' <(free -m)
command give me used memory. I want to get output in valid jason form.
Desired output:
{“Memory”:”80”}
The output of free is piped to a compound command consisting of:
A first "read" which skips the first output line of "free".
A second "read" which reads the line we need, we need only the second and third value.
An echo which prints the line in the format you want including the calculation
Here's an example for memory:
Putting that new file name into json:
Or:
When it gets more complicated than this, you can continue to write line by line or more likely build strings in a variable.
I created a tool called
jc
that converts the output of many command line tools, inclduingfree
to json:https://github.com/kellyjonbrazil/jc