Chris F
Chris F

Reputation: 16673

How to extract a field from each object in an array with jq?

I performed a cURL request to get all the users in our Gitlab server, like this.

$ curl -H "Private-Token: a;sldkfja;slkdfj" https://gitlab.domain.com/api/v3/users?active=true > my_file.json

and (part of) my_file.json looks like:

[
  {
    "username" : "jdoe",
    "name" : "John Doe",
    ...
  },
  {
    "username" : "jadoe",
    "name" : "Jane Doe",
    ...
  }
]

I stored the response in my_file.json. How can I use jq to get all the username values?

$ cat my_file.json | jq -r 'what to put here?'

Upvotes: 77

Views: 55919

Answers (1)

John Kugelman
John Kugelman

Reputation: 361547

$ jq -r '.[].username' my_file.json
jdoe
jadoe

Upvotes: 124

Related Questions