With input.json
:
{
"server1": {
"name": "server1",
"property": "ENABLED"
},
"server2": {
"name": "server2",
"property": "ENABLED"
},
"server3": {
"name": "server3",
"property": "ENABLED"
},
}
The following jq
query:
jq -r '.[] | select(.property == "ENABLED") |.name' input.json
outputs:
server1
server2
server3
I want my output to be without any new line and as below, separated by space and no space after the last word (server3 in this case)
server1 server2 server3
I don't want to do pipe after jq command and obtain the output with sed and tr commands. I want to achieve this with jq command only.
You're pretty close to there! In your case, the join
function may help:
jq -r '[.[]|select(.property=="ENABLED")|.name]|join(" ")' input.json
By wrapping all names in an array, the join
function works like that in python. From the doc:
join(str)
Joins the array of elements given as input, using the argument as separator. It is the inverse of split: that is, running split("foo") | join("foo") over any input string returns said input string.
jq ´join(", ")´ ["a","b,c,d","e"] => "a, b,c,d, e"