Convert arbitrary output to json by column in the terminal?
bash, command-line-interface, json, linux
Solution
I found a great list of tools to work with command line output, and one of the tools listed is sqawk which will convert any arbitrary data to json, and let you filter it using sql like queries!
Convert `ps` output to JSON
ps | sqawk -output json,indent=1 'select PID,TTY,TIME,CMD from a' trim=left header=1
Output
[{
"PID" : "3947",
"TTY" : "pts/2",
"TIME" : "00:00:07",
"CMD" : "zsh"
},{
"PID" : "15951",
"TTY" : "pts/2",
"TIME" : "00:00:00",
"CMD"
}]
Problem
I'd like to be able to pipe the output from any command line program to a command that converts it to json. For example my unknown program could accept target columns, a delimiter and output field names ``` # select columns 1 and 3 from the output and convert it to simple json netstat -a | grep CLOSE_WAIT | convert_to_json 1,3 name,other ``` and would generate something like so: ``` [ {"name": "tcp4", "other": "31"}, {"name": "tcp4", "other": "0"} ... ] ``` I'm looking for something that works for any program, not just `netstat`! I'm open to installing any 3rd party tool/opensource project, and tend to run things on linux/osx - does not have to be a bash script solution, can be written in node, perl, python, etc. EDIT: I'm of course willing to pass in any more info that'd be required to make it work, for example a delimiter or multiple delimiters - I'd just like to avoid explicit parsing in the command line, and have the tool do that.