dsaydon
dsaydon

Reputation: 4769

Linux convert a table from bash command into json

while working with json in windows is very easy in Linux I'm getting trouble.

I found a way to convert list into json using jq:

For example:

ls | jq -R -s -c 'split("\n")'

output:

["bin","boot","dev","etc","home","lib","lib64","media","mnt","opt","proc","root","run","sbin","srv","sys","tmp","usr","var"]

I'm getting trouble to convert a table into json

I'm looking for an option to convert a table that I get from bash command into a json. I already searched for many tools but none of them are generic and you need to adjust the commands to each different table.

Do you know how can I convert a table that I get from a bash commands into json that can be generic?

table output for example:

rpm -qai

output:

Name        : gnome-session 
Version     : 3.8.4 
Release     : 11.el7
Architecture: x86_64 
Install Date: Mon 21 Dec 2015 04:12:41 PM EST
Group       : User Interface/Desktops 
Size        : 1898331 
License     : GPLv2+ 
Signature   : RSA/SHA256, Thu 03 Jul 2014 09:39:10 PM EDT,
Key ID 24c6a8a7f4a80eb5 
Source RPM  : gnome-session-3.8.4-11.el7.src.rpm 
Build Date  : Mon 09 Jun 2014 09:12:26 PM EDT 
Build Host  : worker1.bsys.centos.org 
Relocations : (not relocatable) 
Packager    : CentOS BuildSystem <http://bugs.centos.org> 
Vendor      : CentOS 
URL         : http://www.gnome.org 
Summary     : GNOME session manager 
Description : nome-session manages a GNOME desktop or GDM login session. It starts up the other core GNOME components and handles logout and saving the
session.

Thanks!

Upvotes: 0

Views: 4489

Answers (2)

Duc Vu
Duc Vu

Reputation: 81

You can convert a table from bash command into a json using jq

This command will return a detailed report on the system’s disk space usage

df -h 

The output is something like this

Filesystem       Size   Used  Avail Capacity iused      ifree %iused  Mounted on
/dev/disk3s1s1  926Gi   20Gi  803Gi     3%  502068 4293294021    0%   /
devfs           205Ki  205Ki    0Bi   100%     710          0  100%   /dev
/dev/disk3s6    926Gi  7.0Gi  803Gi     1%       7 8418661400    0%   /System/Volumes/VM
/dev/disk3s2    926Gi  857Mi  803Gi     1%    1811 8418661400    0%   /System/Volumes/Preboot
/dev/disk3s4    926Gi  623Mi  803Gi     1%     267 8418661400    0%   /System/Volumes/Update

Now we can convert the output of this command into json with jq

command=($(df -h | tr -s ' ' | jq -c -Rn 'input  | split(" ") as $head | inputs | split(" ") | to_entries | map(.key = $head[.key]) | from_entries'))
echo $command | jq
{
  "Filesystem": "/dev/disk3s1s1",
  "Size": "926Gi",
  "Used": "20Gi",
  "Avail": "803Gi",
  "Capacity": "3%",
  "iused": "502068",
  "ifree": "4293294021",
  "%iused": "0%",
  "Mounted": "/"
}
{
  "Filesystem": "devfs",
  "Size": "205Ki",
  "Used": "205Ki",
  "Avail": "0Bi",
  "Capacity": "100%",
  "iused": "710",
  "ifree": "0",
  "%iused": "100%",
  "Mounted": "/dev"
}
{
  "Filesystem": "/dev/disk3s6",
  "Size": "926Gi",
  "Used": "7.0Gi",
  "Avail": "803Gi",
  "Capacity": "1%",
  "iused": "7",
  "ifree": "8418536520",
  "%iused": "0%",
  "Mounted": "/System/Volumes/VM"
}
{
  "Filesystem": "/dev/disk3s2",
  "Size": "926Gi",
  "Used": "857Mi",
  "Avail": "803Gi",
  "Capacity": "1%",
  "iused": "1811",
  "ifree": "8418536520",
  "%iused": "0%",
  "Mounted": "/System/Volumes/Preboot"
}

convert table from bash command into json

Upvotes: 1

tripleee
tripleee

Reputation: 189477

There are too many poorly-specified textual formats to create a single tool for what you are asking for, but Unix is well-equipped to the task. Usually, you would create a simple shell or Awk script to convert from one container format to another. Here's one example:

printf '"%s", ' * | sed 's/, $//;s/.*/[ & ]/'

The printf will produce a comma-separated, double-quoted list of wildcard matches. The sed will trim the final comma and add a pair of square brackets around the entire output. The results will be incorrect if a file name contains a double quote, for example, but in the name of simplicity, let's not embellish this any further.

Here's another:

rpm -qai | awk -F ' *: ' 'BEGIN { print "{\n"; }
    { printf "%s\"%s\": \"%s\"", delim, $1, substr($0, 15); delim="\n," }
    END { print "\n}"; }'

The -qf output format is probably better but this shows how you can extract fields from a reasonably free-form line-oriented format using a simple Awk script. The first field before the colon is extracted as the key, and everything from the 15th column onwards is extracted as the value. Again, we ignore the possible complications (double quotes in the values would need to be escaped, again, for example) to keep the example simple.

If your needs are serious, you will need to spend more time on creating a robust parser; but then, you will usually want to work with tools which have a well-defined output format in the first place (XML, JSON, etc) and spend as little time as possible on ad-hoc parsers. Unfortunately, there is still a plethora of tools out there which do not support an --xml or --json output option out of the box, but JSON support is fortunately becoming more widely supported.

Upvotes: 1

Related Questions