I have this shell script here that will go through each line in a json response and fetch me all the siteIds in a single comma delimited String.
input='[
{
"siteIds": "93970,168794,34583,96844,34343,29095,98786,136728,33441,35913",
"status": "Failed for some reason"
},
{
"siteIds": "28709,29810,84178,31027,29985,100487,35486,36068,37001,36360",
"status": "Failed for reason xyz"
},
{
"siteIds": "190873,28714,93455,35995,12542,77957,102080,32006,109115,28902",
"status": "Success"
},
{
"siteIds": "30514,33349,29178,77989,30460,35541,36765,29637,97585,29263",
"status": "Failed:..."
},
{
"siteIds": "36913,36741,30435,35007,109238,30606,33669,76823,75882,30265",
"status": "Success"
}
]'
siteIds=""
while read -r line; do
if [[ $line = *"status\": \"Failed"* ]]; then
echo $line
siteId=$(echo $line | grep -o '"siteIds": "[^"]*"' | awk -F': ' '{print $2}' | tr -d '"')
siteIds="$siteIds$siteId,"
fi
done <<< "$input"
if [[ -z "$siteIds" ]]; then
siteIds = "No failed siteIds found."
fi
echo "${siteIds%?}"
echo "finished"
This is the response I am getting:
"status": "Failed for some reason"
"status": "Failed for reason xyz"
"status": "Failed:..."
,,
finished
How do I get the response to be in this format?
93970,168794,34583,96844,34343,29095,98786,136728,33441,35913,28709,29810,84178,31027,29985,100487,35486,36068,37001,36360,30514,33349,29178,77989,30460,35541,36765,29637,97585,29263
Seems like it is getting the Failed statuses but not able to append the siteIds correctly. I'm not experienced in shell scripts so any help would be appreciated!