Case-insensitive search with JMESPath
Asked Answered
S

1

17

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".

Syncytium answered 15/9, 2021 at 1:54 Comment(1)
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.Syncytium
S
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

Salcido answered 18/12, 2022 at 8:24 Comment(4)
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 :)Syncytium
@Syncytium can you share what was the resolution you had? With case folding?Optime
@Optime 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.Syncytium
Thanks for the update @Syncytium . Appreciate it. I had to drop the JMESPath completely. Looking for an alternative now. JSONata is one - but finding it little difficult/different.Optime

© 2022 - 2024 — McMap. All rights reserved.