0

Given the following Json

{
   "id":"MENU_FULL_V2",
   "version":"MENU_FULL_V2",
   "type":"MAIN",
   "icon":"shopping-bag-r",
   "description":"This is the description {$.description}",
   "order":15000,
   "code":"code",
   "filter":"{$.contractedServices[*].contractId}",
   "status":"ENABLED",
   "contextData":{
      "docType":"{$.documentType}",
      "docNumber":"{$.documentNumber}",
      "organizationName":"{$.name}",
      "contractedServices":"{$.contractedServices[*]}"
   },
   "children":[
      {
         "id":"MENU_CHILD_1",
         "version":"MENU_FULL_V2",
         "type":"CHILD",
         "description":"This is the description {$.description}"
      },
      {
         "id":"MENU_CHILD_2",
         "version":"MENU_FULL_V2",
         "type":"CHILD",
         "description":"This is the description {$.description}"
      }
   ]
}

How can I return a List of string which contains all the paths to the values that has { } in their value.

Is there a way to get all the values that match this regular expresion see

So far I have this.

Configuration conf = Configuration.builder()
   .options(Option.AS_PATH_LIST).build();

List<String> pathList = JsonPath.using(conf).parse(json).read("//GET ALL PATHS THAT MATCH REGEX");

The expected output for pathList should contain all paths that have values that have { }

ie:

"$['description']",
"$['filter']",
"$['contextData']['docType']",
"$['contextData']['docNumber']"

Note that the list is not complete, but it should contain all the paths that have values with { }

Kevin Valdez
  • 359
  • 4
  • 17

1 Answers1

0

You may consider another library Josson to do the transformation.

https://github.com/octomix/josson

Josson josson = Josson.fromJsonString(yourJsonString);

JsonNode node = josson.getNode(
    "flatten('.','[%d]')" +
    ".entries()" +
    ".[value=~'.*\\{.*}.*']*" +
    ".concat('$.',key)");
node.forEach(elem -> System.out.println(elem.asText()));

Output

$.description
$.filter
$.contextData.docType
$.contextData.docNumber
$.contextData.organizationName
$.contextData.contractedServices
$.children[0].description
$.children[1].description
Raymond Choi
  • 1,065
  • 2
  • 7
  • 8