简体   繁体   中英

JsonPath expression for extract two dates

I have a JSON with few date nodes. I am reading the json with the help of JsonPath . is there any way so that we can write expression and get the difference between two dates. My Json is something like below:

"data": [
         {
         "DateA": "2022-11-26Z",
          "otherFactor": 1,
          "DateB": "2022-10-26Z"
         }
        ]

I want to calculate difference between two dates using JsonPath expression. I haven't found anything in documentation https://github.com/json-path/JsonPath

You may consider another library Josson

https://github.com/octomix/josson

Josson josson = Josson.fromJsonString(
    "{\"data\": [" +
    "  {" +
    "    \"DateA\": \"2022-11-26Z\"," +
    "    \"otherFactor\": 1," +
    "    \"DateB\": \"2022-10-26Z\"" +
    "  }" +
    "]}");
JsonNode node = josson.getNode(
    "data.untilInDay(" +
    "  DateB.replace('Z','T00:00Z').offsetToLocalDate()," +
    "  DateA.replace('Z','T00:00Z').offsetToLocalDate()" +
    ")");
System.out.println(node.toPrettyString());

Output

[ 31 ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM