1

I am looking for a JSONPath expression which gets the value for key which is NOT defined strictly, to ignore case sensitivity.

{
    "authors":{
        "Robert.C.M": "Clean Code",
        "Martin.F": "Refactoring"
    }
}

So far, I use following path to get the value:

.authors['Robert.C.M']

which returns "Clean Code". The problem is, that I have been asked to ignore case sensitivity, ie that json can contain "Robert.C.M", "ROBERT.C.M", "robert.c.m". In all cases, I would like to get its value by one query.

In Jayway JSONPath doc I found this query:

$..book[?(@.author =~ /.*REES/i)]

But so far, I am not able to use it for a key definition. I am not even sure if it is possible. The dot "." inside key name is mandatory.


Update: I decided to get all authors by JsonPath

$.properties

and programmatically find a value. A result of JsonPath is converted to a map by our internal library. Then its keys are converted to lower case. A key used to search in map is put to lower case as well.

import java.util.Map;
import java.util.stream.Collectors;

public class Demo {

    public static void main(String[] args) {
        Map<String, String> normalized = Map.of("ROBERT.C.M", "Clean Code")
                .entrySet()
                .stream()
                .collect(Collectors.toMap(
                        e -> e.getKey().toLowerCase(),
                        Map.Entry::getValue)
                );

        String theBook = normalized.get("RoBeRt.C.m".toLowerCase());
        System.out.println("theBook: " + theBook);

    }
}
Lonestar
  • 51
  • 6

0 Answers0