-1

I can get the first and birthday,

{
  "users": [
    {
      "first": "Stevie",
      "last": "Wonder",
      "birthday": "01/01/1945"
    },
    {
      "first": "Michael",
      "last": "Jackson",
      "birthday": "03/23/1963"
    }
  ]
}

So with this jq command, I can get the record:

$ cat a.json |jq '.users[] | .first + " " + .last + " " + .birthday'
"Stevie Wonder 01/01/1945"
"Michael Jackson 03/23/1963"

And I am close to the answer to match the first name

$ cat a.json |jq '.users[] | select(.first=="Stevie") | .birthday '
"01/01/1945"

But how to get the output which matched both first and last name?

Inian
  • 80,270
  • 14
  • 142
  • 161
Bill
  • 2,494
  • 5
  • 26
  • 61
  • 2
    Does this answer your question? [JQ: Select multiple conditions](https://stackoverflow.com/questions/33057420/jq-select-multiple-conditions) – Inian Jul 13 '20 at 06:30
  • 1
    Look up the `jq` manual for the `and` clause – Inian Jul 13 '20 at 06:32

1 Answers1

2

Here is an approach which starts by filtering out .users which do not meet your criteria:

.users |= map(select(
  (.first == "Stevie") and (.last == "Wonder")
))

if you Try it online! you will observe it simplifies your data to just

{
  "users": [
    {
      "first": "Stevie",
      "last": "Wonder",
      "birthday": "01/01/1945"
    }
  ]
}

Then you can add more filters if you want particular elements (e.g. .birthday):

  .users |= map(select(
    (.first == "Stevie") and (.last == "Wonder")
  ))
| .users[].birthday

to obtain Try it online!

"01/01/1945"

This may seem needlessly redundant but may be easier if you are experimenting without precise requirements.

jq170727
  • 13,159
  • 3
  • 46
  • 56