Home > OS >  JQ: Merge all entries in an array without hardcoding the key
JQ: Merge all entries in an array without hardcoding the key

Time:07-24

I have a json like this

[
    {
      "name": "hosts",
      "ipaddress": "1.2.3.4",
      "status": "UP",
      "randomkey": "randomvalue"
    },
    {
      "name": "hosts",
      "ipaddress": "5.6.7.8",
      "status": "DOWN",
      "newkey": "newvalue"
    },
    {
      "name": "hosts",
      "ipaddress": "9.10.11.12",
      "status": "RESTART",
      "anotherkey": "anothervalue"
    }  
]

I want to merge the objects and looking for some output like this

[
  
    {
      "name": "hosts", //doesn't matter if it is ["hosts"]
      "ipaddress": ["1.2.3.4", "5.6.7.8", "9.10.11.12"],
      "status": ["UP", "DOWN", "RESTART"],
      "randomkey": ["randomvalue"],
      "newkey": ["newvalue"],
      "anotherkey": ["anothervalue"]
    }
]

I can hardcode each and every key and do something like this - { ipaddress: (map(.ipaddress) | unique ) } { status: (map(.status) | unique ) } { randomkey: (map(.randomkey) | unique ) } The important ask here is the values are random and cannot be hardcoded. Is there a way i can merge all the keys without hardcoding the key here?

CodePudding user response:

Using reduce, then unique would be one way:

jq '[
  reduce (.[] | to_entries[]) as {$key, $value} ({}; .[$key]  = [$value])
  | map_values(unique)
]'
[
  {
    "name": [
      "hosts"
    ],
    "ipaddress": [
      "1.2.3.4",
      "5.6.7.8",
      "9.10.11.12"
    ],
    "status": [
      "DOWN",
      "RESTART",
      "UP"
    ],
    "randomkey": [
      "randomvalue"
    ],
    "newkey": [
      "newvalue"
    ],
    "anotherkey": [
      "anothervalue"
    ]
  }
]

Demo

Using group_by and map, then unique again would be another:

jq '[
  map(to_entries[]) | group_by(.key)
  | map({key: first.key, value: map(.value) | unique})
  | from_entries
]'
[
  {
    "anotherkey": [
      "anothervalue"
    ],
    "ipaddress": [
      "1.2.3.4",
      "5.6.7.8",
      "9.10.11.12"
    ],
    "name": [
      "hosts"
    ],
    "newkey": [
      "newvalue"
    ],
    "randomkey": [
      "randomvalue"
    ],
    "status": [
      "DOWN",
      "RESTART",
      "UP"
    ]
  }
]

Demo

  • Related