简体   繁体   中英

Extract the key/value pair from all blocks of JSON Array and then append it as new key/value pair in each block of JSON Array using BASH and JQ

I am trying to create a script that extracts a few key/value from all the blocks of an array and then run another shell script using that value to generate a new value and then append that new key/value in all the blocks respectively.

for eg,

serverdetails.json file

{
  "server_details": [
    {
      "seqNo": "01",
      "storage": "IBM",
      "datastore": "IDS-NR02",
      "cpu": 4,
      "site": "SGDC",
      "servertype": "Virtual Machine",
      "location": "Serangoon",
      "operatingsystem": "Linux",
      "systemcode": "BOC",
      "ram": 64,
      "databasesoftware": "MSSQL 2016",
      "usagetype": "Database Server",
      "environment": "Production_WithCustData",
      "domain": "DAPAC",
      "IPAddress": "10.117.254.25",
      "DNS": "10.118.76.34",
      "Gateway": "10.118.76.1",
      "drives": [
        {
          "C": "100"
        },
        {
          "D": "50"
        }
      ]
    },
    {
      "seqNo": "02",
      "storage": "HITACHI",
      "datastore": "HDS-NR02",
      "cpu": 4,
      "site": "SGDR",
      "servertype": "Virtual Machine",
      "location": "DRC",
      "operatingsystem": "Windows",
      "systemcode": "WOC",
      "ram": 64,
      "databasesoftware": "NA",
      "usagetype": "Web Server",
      "environment": "Production_NonCustData",
      "domain": "EDAPAC",
      "IPAddress": "10.117.254.26",
      "DNS": "10.118.76.34",
      "Gateway": "10.118.76.1",
      "drives": [
        {
          "C": "100"
        },
        {
          "D": "50"
        }
      ]
    },
   {
     //can be n no of blocks
   }
  ]
}

Now Let's say block 1 has "site" value as "SGDC", "domain" as "DAPAC", "usagetype" as "Database Server", I will pass these values in another shell script(I ALREADY HAVE THIS Shell SCRIPT) as a parameter to generate a new value "SGDDAPACDS" and use this as a new key/value pair ie "hostname": "SGDDAPACDB" and append it into block 1. Similarly, block 2 will have "hostname": "SGDEDAPACWS". So in the end, the JSON should look like this,

{
  "server_details": [
    {
      "seqNo": "01",
      "storage": "IBM",
      "datastore": "IDS-NR02",
      "cpu": 4,
      "site": "SGDC",
      "servertype": "Virtual Machine",
      "location": "Serangoon",
      "operatingsystem": "Linux",
      "systemcode": "BOC",
      "ram": 64,
      "databasesoftware": "MSSQL 2016",
      "usagetype": "Database Server",
      "environment": "Production_WithCustData",
      "domain": "DAPAC",
      "IPAddress": "10.117.254.25",
      "DNS": "10.118.76.34",
      "Gateway": "10.118.76.1",
      "drives": [
        {
          "C": "100"
        },
        {
          "D": "50"
        }
      ],
      "hostname": "SGDDAPACDS"            //new value
    },
    {
      "seqNo": "02",
      "storage": "HITACHI",
      "datastore": "HDS-NR02",
      "cpu": 4,
      "site": "SGDR",
      "servertype": "Virtual Machine",
      "location": "DRC",
      "operatingsystem": "Windows",
      "systemcode": "WOC",
      "ram": 64,
      "databasesoftware": "NA",
      "usagetype": "Web Server",
      "environment": "Production_NonCustData",
      "domain": "EDAPAC",
      "IPAddress": "10.117.254.26",
      "DNS": "10.118.76.34",
      "Gateway": "10.118.76.1",
      "drives": [
        {
          "C": "100"
        },
        {
          "D": "50"
        }
      ],
      "hostname": "SGDEDAPACWS"       //new value
    },
   {
     //can be n no of blocks
   }
  ]
}

Thanks in advance

I am able to resolve it via script. Basically, I was struggling with single quotes and double qoutes.

Below is the sample script

#!/bin/bash

if [ -f ./serv_details.json ]; 
then 
    arrayCount=`jq '.server_details | length' ./serv_details.json`
    for (( ac=0; ac<arrayCount; ac++ ))
    do 
        SITE=$(jq -r ".server_details[$ac].site" serv.json)
        LOC=$(jq -r ".server_details[$ac].environment" serv.json )
        SEQ=$(jq -r ".server_details[$ac].seqNo" serv.json)
        HO=`./hostsample.sh $SITE $LOC $SEQ`;   # It generates the concated Value
        echo $HO
        cat serv.json | jq '.server_details['"$ac"'] += {"hostname": "'"$HO"'"}' | sponge serv.json
        
    done
else
    echo "pls pass the value file"
fi

Then the output will be

{
  "server_details": [
    {
      "seqNo": "01",
      "storage": "IBM",
      "datastore": "IDS-G400-VS-DC-DE-T2-P01-NR02",
      "cpu": 4,
      "site": "SGDC",
      "servertype": "Virtual Machine",
      "location": "Serangoon",
      "operatingsystem": "Linux",
      "systemcode": "BOC",
      "ram": 64,
      "databasesoftware": "MSSQL 2016",
      "usagetype": "Database Server",
      "environment": "Production_WithCustData",
      "domain": "DAPAC",
      "IPAddress": "10.117.254.25",
      "DNS": "10.118.76.34",
      "Gateway": "10.118.76.1",
      "drives": [
        {
          "C": "100"
        },
        {
          "D": "50"
        }
      ],
      "hostname": "SGDS01"
    },
    {
      "seqNo": "02",
      "storage": "HITACHI",
      "datastore": "HDS-G400-VS-DC-DE-T2-P01-NR02",
      "cpu": 4,
      "site": "SGDR",
      "servertype": "Virtual Machine",
      "location": "DRC",
      "operatingsystem": "Windows",
      "systemcode": "WOC",
      "ram": 64,
      "databasesoftware": "NA",
      "usagetype": "Web Server",
      "environment": "Production_NonCustData",
      "domain": "DAPAC",
      "IPAddress": "10.117.254.26",
      "DNS": "10.118.76.34",
      "Gateway": "10.118.76.1",
      "drives": [
        {
          "C": "100"
        },
        {
          "D": "50"
        }
      ],
      "hostname": "SGDD02"
    }
  ]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM