Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get key value of matching case insensitive string

Tags:

json

jq

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.

like image 765
David Biers Avatar asked Dec 22 '22 19:12

David Biers


1 Answers

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
like image 183
oguz ismail Avatar answered Jan 09 '23 17:01

oguz ismail