Remove duplicates in JSON values ​​using jq

I have the following JSON:

[
  {
    "function": "ping",
    "name": "start",
    "servers": [
      {
        "load": 581.6875,
        "last_heard": 2.379324197769165,
        "version": "1.0",
        "hidden": false,
        "pid": "19735"
      },
      {
        "load": 444.0625,
        "last_heard": 1.3227169513702393,
        "version": "1.0",
        "hidden": false,
        "pid": "12092"
      }
    ]
  },
  {
    "function": "pong",
    "name": "middle",
    "servers": [
      {
        "load": 581.6875,
        "last_heard": 2.379324197769165,
        "version": "2.0",
        "hidden": false,
        "pid": "19735"
      },
      {
        "load": 444.0625,
        "last_heard": 1.3227169513702393,
        "version": "3.0",
        "hidden": false,
        "pid": "12092"
      },
      {
        "load": 444.0625,
        "last_heard": 1.3227169513702393,
        "version": "3.0",
        "hidden": false,
        "pid": "12093"
      }
    ]
  },
  {
    "function": "pang",
    "name": "end",
    "servers": [
      {
        "load": 581.6875,
        "last_heard": 2.379324197769165,
        "version": "2.0",
        "hidden": false,
        "pid": "19735"
      },
      {
        "load": 444.0625,
        "last_heard": 1.3227169513702393,
        "version": "2.0",
        "hidden": false,
        "pid": "12092"
      }
    ]
  }
]

(this is just a sample, this is hundreds of records)

I need to get

[{"name": "start", "version": ["1.0"]},
{"name": "middle", "version": ["2.0", "3.0"]},
{"name": "end", "version": ["2.0"]}]

Therefore, I need to delete useless data, and then get a list of names with their unique values ​​for the "version".

I can get to the point where I have

{
  "name": "ping",
  "version": [
    "1.0",
    "1.0"
  ]
}
{
  "name": "pong",
  "version": [
    "2.0",
    "3.0",
    "3.0"
  ]
}
{
  "name": "pang",
  "version": [
    "2.0",
    "2.0"
  ]
}

using

jq '.[] | {name: .function, version: [.servers[].version]}'

But I need to get rid of duplicate values. Is this possible with jq?

+4
source share
2 answers
You were almost there. Just connect the array versionto the function unique:
jq '[.[]|{name, "version": [.servers[].version]|unique}]' input
+6
source

,

 [
   . []
 | reduce .servers[].version as $v (
     {name, version:{}}
   ; .version[$v] = 1
   )
 | .version |= keys
 ]
0

Source: https://habr.com/ru/post/1616282/


All Articles