Search code examples
jsonuniquejq

jq get all unique values for a given key in a list of objects


Let's say I have an endpoint that returns the following array:

[
  {"name": "Joe", "age": 21},
  {"name": "Steve", "age": 27},
  {"name": "Michelle", "age": 32},
  {"name": "Joe", "age": 23},
]

I know I can get all names using the following command (using httpie):

http https://some-endpoint | jq '.[] | .name'

# output
Joe
Steve
Michelle
Joe

How can I get all unique names (so there are no duplicates)?


Solution

  • Assuming the input is valid JSON, the following jq program will yield an array of the distinct names:

    map(.name) | unique
    

    If the input has superfluous commas as in the sample shown, you might wish to consider using a preprocessor, such as any-json or hjson.