Search code examples
javajsonpath

Filter with addition or subtraction condition


I have a simple json payload. I would like to add a filter condition on this which checks if (mode == 'custom' and lastUpdatedDate - createdDate >= 180000).

Can someone help with the filter condition.

{
   "id":"1664437560",
   "mode":"CUSTOM",
   "createdDate":1664437561000,
   "lastUpdatedDate":1664437620256,
   "customerIdentifier":"8a9fcc828",
   "status":"Success"
}

p.s -- I am familiar with jsonpath but, could not find a way to have a filter condition with +/-/* operators. I also tried to add filter condition on sum function,which also didn't work.


Solution

  • Library Josson can do the job.

    https://github.com/octomix/josson

    Deserialization

    Josson josson = Josson.fromJsonString(
        "{" +
        "   \"id\":\"1664437560\"," +
        "   \"mode\":\"CUSTOM\"," +
        "   \"createdDate\":1664437561000," +
        "   \"lastUpdatedDate\":1664437620256," +
        "   \"customerIdentifier\":\"8a9fcc828\"," +
        "   \"status\":\"Success\"" +
        "}");
    

    Query

    JsonNode node = josson.getNode("[mode='CUSTOM' & calc(lastUpdatedDate - createdDate) >= 180000]");
    System.out.println(node == null ? null : node.toPrettyString());
    

    Output

    null
    

    Deserialization (Changed lastUpdatedDate to 1664437861000)

    Josson josson = Josson.fromJsonString(
        "{" +
        "   \"id\":\"1664437560\"," +
        "   \"mode\":\"CUSTOM\"," +
        "   \"createdDate\":1664437561000," +
        "   \"lastUpdatedDate\":1664437861000," +
        "   \"customerIdentifier\":\"8a9fcc828\"," +
        "   \"status\":\"Success\"" +
        "}");
    

    Query

    JsonNode node = josson.getNode("[mode='CUSTOM' & calc(lastUpdatedDate - createdDate) >= 180000]");
    System.out.println(node == null ? null : node.toPrettyString());
    

    Output

    {
      "id" : "1664437560",
      "mode" : "CUSTOM",
      "createdDate" : 1664437561000,
      "lastUpdatedDate" : 1664437861000,
      "customerIdentifier" : "8a9fcc828",
      "status" : "Success"
    }