Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jq get unique value from two keys

Tags:

json

unique

jq

I know to get a unique from one key - unique_by('.[].name)

I want to get output by checking for unique values in two keys

but how to do for two keys like unique_by('.[].name,.[].url') and return the input along with other keys?

#input

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

#expected output

[
  {
    "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"
  }
]
like image 834
kingsec Avatar asked Oct 29 '25 01:10

kingsec


2 Answers

Collect the criteria into an array:

unique_by([.name, .url]) 
like image 64
peak Avatar answered Oct 31 '25 01:10

peak


Just provide to unique_by an array with everything included, so that the array must become unique:

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

like image 21
pmf Avatar answered Oct 31 '25 01:10

pmf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!