jq group by and Increment at same time

后端 未结 2 1741
迷失自我
迷失自我 2021-01-16 14:06

Sample input1.json


[{
    \"organizationId\": \"org1\",
    \"status\": \"UP\",
    \"server\": \"server1\"         


        
2条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-16 14:16

    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"
      }
    ]
    

提交回复
热议问题