I need to generate JSON output from my shell script.
I need to get Ram slot details of a particular machine and generate JSON using those details.
To get Ram details I am using system_profiler SPMemoryDataType
It produces details as follows.
BANK 0/DIMM0:
Size: 2 GB
Type: DDR3
Speed: 1600 MHz
Status: OK
Manufacturer: 0x802C
Part Number: 0x384A54463235363634485A2D3147364D3120
Serial Number: 0xE98388E6
BANK 1/DIMM0:
Size: 2 GB
Type: DDR3
Speed: 1600 MHz
Status: OK
Manufacturer: 0x802C
Part Number: 0x384A54463235363634485A2D3147364D3120
Serial Number: 0xE98388E5
From that I should form JSON like this
[
{"Bank":"0/DIMM0","Serial Number":"0xE98388E6","Status":"OK"},
{"Bank":"1/DIMM0","Serial Number":"0xE98388E5","Status":"OK"}
]
To extract separate details like bank, Serial Number, Status we can use
system_profiler SPMemoryDataType | awk '/Bank/
system_profiler SPMemoryDataType | awk '/Serial/
system_profiler SPMemoryDataType | awk '/Status/
I am sure that there is a need of Dynamic variable to do form json from the results. But since I am new to shell script I am confused. Is there any way to generate JSON from the output?