javajsonpathjayway

How to filter the data according to recent datetime in JSON Array - JAVA?


Here is the sample JSON:

{
  "items": [
    {
      "id": "3655",
      "address": "+911234567890",
      "date": "1627831097120",
      "read": "0",
      "status": "-1",
      "type": "1",
      "body": "Demo 2",
      "serviceCenter": "+911234567890"
    },
    {
      "id": "3654",
      "address": "+911234567890",
      "date": "1627830900372",
      "read": "0",
      "status": "-1",
      "type": "1",
      "body": "Demo1",
      "serviceCenter": "+911234567890"
    },
    {
      "id": "3654",
      "address": "+910000000000",
      "date": "1627831097460",
      "read": "0",
      "status": "-1",
      "type": "1",
      "body": "Demo1",
      "serviceCenter": "+9110000000000"
    }
  ]
}

I am using Jayway JsonPath for querying the data and below is the sample code.

List<Map<String, Object>> d = JsonPath.parse(strJSON).read("$.items[?(@.address=='+911234567890')]");
for (Map<String, Object> resultMap : results) {

    long milliSeconds = Long.parseLong((String) resultMap.get("date"));
    Instant instant = Instant.ofEpochMilli(milliSeconds);
    ZoneId zoneId = ZoneId.systemDefault();
    ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(instant, zoneId);
    
    if (zonedDateTime.getLong(ChronoField.MINUTE_OF_HOUR) <= 3) { 
        System.out.println("Message received within 3 minutes \t" + resultMap);
    } else {
        System.out.println("Message received before 3 minutes \t" + resultMap);
    }
}

With the above given code, I am filtering the record that has been received within 3 minutes. But sometimes I am getting multiple records

Results:

Message received before 3 minutes   {id=3655, address=+911234567890, person=null, date=1627831097120, read=0, status=-1, type=1, subject=null, body=Demo 2, serviceCenter=+911234567890}
Message received before 3 minutes   {id=3654, address=+911234567890, person=null, date=1627830900372, read=0, status=-1, type=1, subject=null, body=Demo1, serviceCenter=+911234567890}

I want the recent record which is

Message received before 3 minutes   {id=3655, address=+911234567890, person=null, date=1627831097120, read=0, status=-1, type=1, subject=null, body=Demo 2, serviceCenter=+911234567890}

So how can I achieve the same by recent dateandtime. Please help me.


Solution

  • Method 1

    Keep track of the record with the largest (most recent) timestamp in the loop itself.

    List<Map<String, Object>> d = JsonPath.parse(strJSON).read("$.items[?(@.address=='+911234567890')]");
    Map<String, Object> mostRecentTimestampItem = new HashMap<>();
    long mostRecentTimestamp = 0;
    for (Map<String, Object> resultMap : d) {
        long milliSeconds = Long.parseLong((String) resultMap.get("date"));
        if (mostRecentTimestamp < milliSeconds) {
             mostRecentTimestamp = milliSeconds;
             mostRecentTimestampItem = resultMap;
        }
    }
    System.out.println("Most recent record = " + mostRecentTimestampItem);
    

    Output of the above code snippet is :-

    Most recent record = {id=3655, address=+911234567890, date=1627831097120, read=0, status=-1, type=1, body=Demo 2, serviceCenter=+911234567890}
    

    Method 2

    We can implement a custom Comparator which sorts the List<Map<String, Object>> in descending order of their "date" value. The most recent record will be the first record in the sorted List.

    List<Map<String, Object>> resultMapsList = JsonPath.parse(strJSON).read("$.items[?(@.address=='+911234567890')]");
    
    System.out.println("ORIGINAL MAPS LIST = " + resultMapsList);
    
    Collections.sort(resultMapsList, (o1, o2) -> Long.parseLong(o1.get("date").toString()) < Long.parseLong(o2.get("date").toString()) ? 1 : -1);
    
    System.out.println("SORTED MAPS LIST = " + resultMapsList);
    System.out.println("MOST RECENT RECORD = " + resultMapsList.get(0));
    

    Output of the above code snippet is :-

    ORIGINAL MAPS LIST = [{"id":"3655","address":"+911234567890","date":"1627831097120","read":"0","status":"-1","type":"1","body":"Demo 2","serviceCenter":"+911234567890"},{"id":"3654","address":"+911234567890","date":"1627830900372","read":"0","status":"-1","type":"1","body":"Demo1","serviceCenter":"+911234567890"}]
    SORTED MAPS LIST = [{"id":"3655","address":"+911234567890","date":"1627831097120","read":"0","status":"-1","type":"1","body":"Demo 2","serviceCenter":"+911234567890"},{"id":"3654","address":"+911234567890","date":"1627830900372","read":"0","status":"-1","type":"1","body":"Demo1","serviceCenter":"+911234567890"}]
    MOST RECENT RECORD = {id=3655, address=+911234567890, date=1627831097120, read=0, status=-1, type=1, body=Demo 2, serviceCenter=+911234567890}