I would like improve my shell script and print a good values using JQ I'm get results from sonarqube api.
In using this script.
projectStatusUrl='url/api/qualitygates/project_status?projectKey=app'
project_status="$(curl -s -u token: -G --data-urlencode --data-urlencode ${projectStatusUrl})"
code_T=$(jq -r '.projectStatus.conditions[] | select(.status=="ERROR") | .status,.metricKey,.comparator' <<< "$project_status")
echo "$code_T"
with a this script I'm getting this results
ERROR
new_coverage
LT
ERROR
new_duplicated_lines_density
GT
I would like improve this format for example add text before values.
Status: ERROR
MetricKey: new_coverage
Comparator: LT
Status: ERROR
MetricKey: new_duplicated_lines_density
Comparator: GT
I trying using this code below
code_T=$(jq -r '.projectStatus.conditions[] | select(.status=="ERROR") | {Status:.status, MetricKey:.metricKey,Comparator:.comparator}' <<< "$project_status")
but my result is
{
"Status": "ERROR",
"MetricKey": "new_coverage",
"Comparator": "LT"
}
{
"Status": "ERROR",
"MetricKey": "new_duplicated_lines_density",
"Comparator": "GT"
}
CodePudding user response:
The {Status:.status, MetricKey:.metricKey,Comparator:.comparator}
you've used creates JSON objects. What you seem to be looking for is just string concatenation:
"Status: " .status, "MetricKey: " .metricKey, "Comparator: " .comparator