jq group by and Increment at same time

后端 未结 2 1740
迷失自我
迷失自我 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"
      }
    ]
    
    0 讨论(0)
  • 2021-01-16 14:35

    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"
      }
    ]
    
    0 讨论(0)
提交回复
热议问题