Search code examples
jsonpath

Building a Filter in a loop, JSONPath


I want loop through an array of strings and add each string to a JsonPath filter object:

Filter f = filter(); // ERROR
for (int j = 0; j < in_paths.size(); j++) 
{
      f = f.and(where(in_paths[j]).exists(true));
}

The only problem with this approach is that I can't seem to create an "empty" filter. Does anyone know if this is possible? If not, can someone offer an alternative?


Solution

  • The initial filter can be a check to see if the document root exists.

    Filter f = filter(where("$").exists(true));
    for (int j = 0; j < in_paths.size(); j++) 
    {
          f = f.and(where(in_paths[j]).exists(true));
    }