David Biers
David Biers

Reputation: 55

Get key value of matching case insensitive string

I'm attempting to pull some specific data from the "id" field but jq matches are being case sensitive and creating an issue with the searches (not matching, basically, so returning 0 results).

JSON Example:

{
  "created_at": "2020-01-17T12:54:02Z",
  "primary": true,
  "verified": true,
  "deliverable_state": "deliverable",
  "undeliverable_count": 0,
  "updated_at": "2020-01-17T12:54:03Z",
  "url": "http://www.website.com",
  "id": 376062709553,
  "user_id": 374002305374,
  "type": "email",
  "value": "[email protected]"
}
{
  "created_at": "2019-02-07T20:49:41Z",
  "primary": false,
  "verified": true,
  "deliverable_state": "deliverable",
  "undeliverable_count": 0,
  "updated_at": "2019-02-07T20:49:41Z",
  "url": "http://www.website.com",
  "id": 366235941554,
  "user_id": 374002305374,
  "type": "email",
  "value": "[email protected]"
}

When running jq against the following, I get the correct return:

$ jq -r '. | select(.value=="[email protected]") .id' sample.json
366235941554

But if I run with the incorrect case, eg.

$ jq -r '. | select(.value=="[email protected]") .id' sample.json

...I do not get a result. I've read through some of the documentation but unfortunately, I do not understand the test/match/capture flags for insensitive search (https://stedolan.github.io/jq/manual/#RegularexpressionsPCRE) or how to get it to operate with this "locate and give me the value" request.

I've searched seemingly everywhere but I'm unable to find any examples of this being used.

Upvotes: 4

Views: 2480

Answers (1)

oguz ismail
oguz ismail

Reputation: 50795

You don't really need regexp for this.

select(.value | ascii_downcase == "[email protected]") .id

But if you insist on it, below is how you perform a case-insensitive match using test/2.

select(.value | test("[email protected]"; "i")) .id

Upvotes: 10

Related Questions