[英]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
我正在尝试创建一个脚本,该脚本从数组的所有块中提取一些键/值,然后使用该值运行另一个 shell 脚本以生成一个新值,然后分别在所有块中附加该新键/值。
例如,
serverdetails.json 文件
{
"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
}
]
}
现在假设块 1 的“站点”值为“SGDC”,“域”为“DAPAC”,“usagetype”为“数据库服务器”,我将在另一个 shell 脚本中传递这些值(我已经有了这个 shell 脚本)作为用于生成新值“SGDDAPACDS”的参数并将其用作新的键/值对,即“主机名”:“SGDDAPACDB”并将其附加到块 1 中。类似地,块 2 将具有“主机名”:“SGDEDAPACWS”。 所以最后,JSON 应该是这样的,
{
"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
}
]
}
提前致谢
我可以通过脚本解决它。 基本上,我在单引号和双引号中挣扎。
下面是示例脚本
#!/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
然后输出将是
{
"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"
}
]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.