56

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?'
chepner
  • 497,756
  • 71
  • 530
  • 681
Chris F
  • 14,337
  • 30
  • 94
  • 192
  • 1
    If that is what the actual cURL response or contents of your file looks like, you do not have valid JSON. – JAAulde Jan 24 '18 at 15:35
  • 1
    You showed us the source data, what are you wanting the output data to look like? – JAAulde Jan 24 '18 at 15:38
  • @JAAulde, I want an output that can let me parse all the usernames easily. – Chris F Jan 24 '18 at 15:53
  • Your stated goal is entirely too subjective. Please provide a sample format of desired output in your question. – JAAulde Jan 24 '18 at 16:24
  • 16
    I don't think this is a bad request at all. The precise output format doesn't matter - he's obviously trying to grapple with the jq syntax to (as the title says) "extract a field from each object in an array". The accepted answerer clearly figured out what he (and I) wanted. – fool4jesus Jun 28 '19 at 12:09

1 Answers1

92
$ jq -r '.[].username' my_file.json
jdoe
jadoe
John Kugelman
  • 349,597
  • 67
  • 533
  • 578