json jq从两个键中获取唯一值

xqkwcwgp  于 2023-04-08  发布在  其他
关注(0)|答案(2)|浏览(105)

我知道从一个键中获取唯一值-unique_by('.[].name)
我想通过检查两个键中的唯一值来获取输出
但是如何处理像unique_by('.[].name,.[].url')这样的两个键并返回输入沿着其他键呢?

输入

[
  {
    "name": "abc",
    "url": "https://aa.com",
    "created_at": "2022-09-30T11:17:33.181Z"
  },
  {
    "name": "bb",
    "url": "https://ddd.com",
    "created_at": "2022-09-30T11:14:33.180Z"
  },
  {
    "name": "abc",
    "url": "https://aa.com",
    "created_at": "2022-09-30T11:14:33.180Z"
  }
]

预期输出

[
  {
    "name": "abc",
    "url": "https://aa.com",
    "created_at": "2022-09-30T11:17:33.181Z"
  },
  {
    "name": "bb",
    "url": "https://ddd.com",
    "created_at": "2022-09-30T11:14:33.180Z"
  }
]
7vhp5slm

7vhp5slm1#

将条件收集到数组中:

unique_by([.name, .url])
nfs0ujit

nfs0ujit2#

只需向unique_by提供一个包含所有内容的数组,以便该数组必须是唯一的:

jq 'unique_by([.name, .url])'
[
  {
    "name": "abc",
    "url": "https://aa.com",
    "created_at": "2022-09-30T11:17:33.181Z"
  },
  {
    "name": "bb",
    "url": "https://ddd.com",
    "created_at": "2022-09-30T11:14:33.180Z"
  }
]

Demo

相关问题