Sample Header Ad - 728x90

Make JSON from SQL query output

3 votes
2 answers
2587 views
I have this database query:
select hostname,size from tableinfo
The output is like this:
hostname                   size
------------------------- -----------
  host1                        28
  host2                        13
  host3                        79
  host4                        28
  host5                        17
Or, I can make it like this:
host1                        28
host2                        13
host3                        79
host4                        28
host5                        17
I want to write a shell script that converts this output to JSON, but I don't really know where to begin or what to do. The JSON must be like this:
{
    "data":[
    {  "{#HOSTNAME}":"host1",  "{#SIZE}":"28"  } ,
	{  "{#HOSTNAME}":"host2",  "{#SIZE}":"13"  } ,
	{  "{#HOSTNAME}":"host3",  "{#SIZE}":"79"  } ,
	{  "{#HOSTNAME}":"host4",  "{#SIZE}":"28"  } ,
	{  "{#HOSTNAME}":"host5",  "{#SIZE}":"17"  }
    ]
}
Asked by BlackCrystal (786 rep)
Jan 19, 2019, 10:25 AM
Last activity: Nov 10, 2023, 03:28 PM