Home > Software engineering >  Apply some condition on jsonnode and filter resultset in Java
Apply some condition on jsonnode and filter resultset in Java

Time:09-29

I have an array of Jsonnodes. something like that

[
 {
   "coupon": "VAR",
   "currency": "USD",
   "sip": "94989WAX5",
   "lastModifiedDate": "2022-09-23T08:16:25Z"
  },
  {
   "coupon": "VAR1",
   "currency": "USD",
   "sip": "94989WAX5",
   "lastModifiedDate": "2022-09-21T08:16:25Z"
  },
  {
   "coupon": "VAR3",
   "currency": "USD",
   "sip": "XHBRYWEB1",
   "lastModifiedDate": "2022-09-20T08:16:25Z"
  }
 ]

I have a requirement, if the sip value of two nodes are same then I need to pick only that sip which lastModifiedDate is latest. In above example the final output should be remaining two nodes.

 [
  {
   "coupon": "VAR",
   "currency": "USD",
   "sip": "94989WAX5",
   "lastModifiedDate": "2022-09-23T08:16:25Z"
  },
  {
   "coupon": "VAR3",
   "currency": "USD",
   "sip": "XHBRYWEB1",
   "lastModifiedDate": "2022-09-20T08:16:25Z"
  }
 ]

I was try to solve it by creating HashMap<String,JsonNode> where Sip is the key and the JsonNode is complete node. It doesn't seems to be a cleaner way. is there any other way to achieve it. I am using fasterxml.jackson.databind.JsonNode

CodePudding user response:

Map<String, JsonNode> map =
    jsonNodeList.stream()
        .collect(
            toMap(
                jsonNode -> jsonNode.get("sip").asText(),
                jsonNode -> jsonNode,
                (jsonNode1, jsonNode2) -> {
                  boolean after =
                      LocalDateTime.parse(String.valueOf(jsonNode1.get("lastModifiedDate")))
                          .isAfter(
                              LocalDateTime.parse(
                                  String.valueOf(jsonNode2.get("lastModifiedDate"))));
                  return after ? jsonNode1 : jsonNode2;
                },
                HashMap::new));

CodePudding user response:

https://github.com/octomix/josson

Deserialization

Josson josson = Josson.fromJsonString(
    "["  
    " {"  
    "   \"coupon\": \"VAR\","  
    "   \"currency\": \"USD\","  
    "   \"sip\": \"94989WAX5\","  
    "   \"lastModifiedDate\": \"2022-09-23T08:16:25Z\""  
    "  },"  
    "  {"  
    "   \"coupon\": \"VAR1\","  
    "   \"currency\": \"USD\","  
    "   \"sip\": \"94989WAX5\","  
    "   \"lastModifiedDate\": \"2022-09-21T08:16:25Z\""  
    "  },"  
    "  {"  
    "   \"coupon\": \"VAR3\","  
    "   \"currency\": \"USD\","  
    "   \"sip\": \"XHBRYWEB1\","  
    "   \"lastModifiedDate\": \"2022-09-20T08:16:25Z\""  
    "  }"  
    " ]");

Transformation

JsonNode node = josson.getNode("group(sip)@.elements.findByMax(lastModifiedDate)");
System.out.println(node.toPrettyString());

Output

[ {
  "coupon" : "VAR",
  "currency" : "USD",
  "sip" : "94989WAX5",
  "lastModifiedDate" : "2022-09-23T08:16:25Z"
}, {
  "coupon" : "VAR3",
  "currency" : "USD",
  "sip" : "XHBRYWEB1",
  "lastModifiedDate" : "2022-09-20T08:16:25Z"
} ]
  • Related