Sample input1.json
[{
\"organizationId\": \"org1\",
\"status\": \"UP\",
\"server\": \"server1\"
jq
solution:
jq 'group_by(.organizationId)
| map(reduce .[] as $o ({"Down_Count" : 0};
if $o["status"] == "DOWN" then .Down_Count += 1 else . end
| . + { ($o["server"]) : $o["status"],
"organizationId" : $o["organizationId"] }
))' input.json
The output:
[
{
"Down_Count": 2,
"server1": "UP",
"organizationId": "org1",
"server2": "DOWN",
"server3": "DOWN"
},
{
"Down_Count": 0,
"server1": "UP",
"organizationId": "org2",
"server2": "UP"
}
]
Here's another approach you could take:
group_by(.organizationId) | map(
reduce ([., [range(length)]] | transpose[]) as [$o,$i] (
{
organizationId: .[0].organizationId,
Down_Count: (map(select(.status=="DOWN")) | length)
};
reduce $keys[] as $k (
.["server\($i+1)"] = $o.status;
if $o | has($k) then .[$k] = $o[$k] else . end
)
)
)
Just pass in an array of keys named $keys
you want to be copied over. If it exists in one of the objects, it will be copied to the result object.
$ cat input.json
[{
"organizationId": "org1",
"status": "UP",
"server": "server1",
"somekey1" : "somevalue1"
},
{
"organizationId": "org1",
"status": "DOWN",
"server": "server2",
"somekey2" : "somevalue2"
},
{
"organizationId": "org1",
"status": "DOWN",
"server": "server3"
},
{
"organizationId": "org2",
"server": "server1",
"status": "UP",
"somekey2" : "somevalue2"
},
{
"organizationId": "org2",
"server": "server2",
"status": "UP",
"somekey4" : "somevalue4"
}]
$ cat program.jq
group_by(.organizationId) | map(
reduce ([., [range(length)]] | transpose[]) as [$o,$i] (
{
organizationId: .[0].organizationId,
Down_Count: (map(select(.status=="DOWN")) | length)
};
reduce $keys[] as $k (
.["server\($i+1)"] = $o.status;
if $o | has($k) then .[$k] = $o[$k] else . end
)
)
)
$ jq --argjson keys '["somekey2"]' -f program.jq input.json
[
{
"organizationId": "org1",
"Down_Count": 2,
"server1": "UP",
"server2": "DOWN",
"somekey2": "somevalue2",
"server3": "DOWN"
},
{
"organizationId": "org2",
"Down_Count": 0,
"server1": "UP",
"somekey2": "somevalue2",
"server2": "UP"
}
]