0

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

Sumit Sood
  • 441
  • 7
  • 23

2 Answers2

1
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));
  • I really dislike Java 8. It's supposed to be more readable, but all I see is one long run-on sentence. – WPW Sep 28 '22 at 16:27
  • what? it is a declarative method chaining. and what version of Java are you programming on? – Pavlo Kuchereshko Sep 28 '22 at 16:35
  • 11, but I never use the Java 8 stuff, all my colleagues do. It's not more readable, it's like can I please get a statement termination and a line break please, you know so I can catch my breath. – WPW Sep 28 '22 at 16:41
  • Stream API was provided not because of readability reasons and it is pretty readable if you know what is what and how it works IMHO. what programming languages have you ever used apart from Java? btw I like @Raymond Choi's answer – Pavlo Kuchereshko Sep 28 '22 at 17:19
  • Yes I know, I doubt the development team at Oracle would make that the driving force behind their proposals. – WPW Sep 28 '22 at 17:23
  • Yours is a good answer too, I just don't like Java 8. My colleagues would praise you so much for your Java 8 code, also I work with JavaScript/React there's a lot of syntax like that too. map, filter, some etc... – WPW Sep 28 '22 at 17:26
  • well, it's a bit of functional programming and declarative approach in the imperative language that Java actually is, It's compact now, add a self-explaining method name or even a small comment taking into account most of all a quite low possibility that someone would have to dig again around this code next N months or years – Pavlo Kuchereshko Sep 28 '22 at 17:37
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/248438/discussion-between-pavlo-kuchereshko-and-wpw). – Pavlo Kuchereshko Sep 29 '22 at 10:45
1

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"
} ]
Raymond Choi
  • 1,065
  • 2
  • 7
  • 8