16

The question is simple. How to do a case-insensitive searching with JMESPath?

Let's say to search for foo in this JSON:

[
  "foo",
  "foobar",
  "barfoo",
  "bar",
  "baz",
  "barbaz",
  "FOO"
]

Here is the case-sensitive search query:

[?contains(@, 'foo')]

It will returns ["foo", "foobar", "barfoo"] but it misses "FOO".

dreftymac
  • 31,404
  • 26
  • 119
  • 182
HKTonyLee
  • 3,111
  • 23
  • 34
  • 2
    I have looked up in JMESPath official tutorial, reference, Google and Stackoverflow. But I could not find any related information / function / sample. So I am wondering if this is possible. – HKTonyLee Sep 15 '21 at 01:54

1 Answers1

3
[?contains(lower(@), lower('string_you_want_to_search')]

We could convert both search strings into lower or upper case to get case insensitive search.

https://jmespath.site/#preview Latest preview has support for it.

JEP discussion https://github.com/jmespath-community/jmespath.spec/discussions/21

Ganesh
  • 285
  • 1
  • 3
  • 10
  • Thanks for your answer @Ganesh! Lower is useful for case insensitive comparison in many situations. But not enough for some characters. I have followed up in the JEP discussion (to add case folding - the proper way to do case insensitive comparison). Anyway thanks for the answer :) – HKTonyLee Dec 19 '22 at 18:22
  • @HKTonyLee can you share what was the resolution you had? With case folding? – KManish Mar 08 '23 at 13:13
  • @KManish Sorry I still haven't found a satisfactory solution. Although lower() function comes closest, but it is still in preview and may not be available in existing services or products. – HKTonyLee Mar 08 '23 at 19:20
  • Thanks for the update @HKTonyLee . Appreciate it. I had to drop the JMESPath completely. Looking for an alternative now. JSONata is one - but finding it little difficult/different. – KManish Mar 11 '23 at 04:55