0

I have two JSON files, called "pickevent1" and "pickevent2". I have to compare if both files are matching; if they don't match, I need to know where they don't match.

pickevent1

 {
     "pickEventActivities": [{
         "orderId": "215",
         "lineNbr": 0,
         "pick": "EACH",
         "activations": [{
             "activationType": "Si",
             "activationValue": "31"
         }]
      }]
  }

pickevent2

{
    "pickEventActivities": [{
        "orderId": "115",
        "lineNbr": 1,
        "pick": "Hello",
        "activations": [{
            "activationType": "Bi",
            "activationValue": "3"
        }]
    }]
}

I created a pick event POJO class:

@JsonRootName(value = "pickEventActivities")
@Data
@JsonPropertyOrder({ "orderId", "lineNbr", "pick"})
class PickEvent {
    String orderId;
    String lineNbr;
    String pick;
    List<Activation> activations;
}

and a Activation POJO class:

@Data
@JsonPropertyOrder({ "activationType", "activationValue"})
public class Activation {
    String activationType;
    String activationValue;
}

To make sure it works, I created a test class:

public void compareJson() throws Exception {
    final ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.configure(FAIL_ON_UNKNOWN_PROPERTIES, false);
    objectMapper.configure(ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
    
    PickEvent result1 = objectMapper.readValue(new File("src/../pickevent1.json"), PickEvent.class);
    PickEvent result2 = objectMapper.readValue(new File("src/../pickevent2.json"), PickEvent.class);
    
    assertEquals(result1, result2);
}

But when I am doing assertSame(result1,result2) its giving me null for json values:

Exception in thread "main" java.lang.AssertionError: expected same:<PickEvent(orderId=null, lineNbr=null, pick=null, activations=null)> was not:<PickEvent(orderId=null, lineNbr=null, pick=null, activations=null)>
    at org.junit.Assert.fail(Assert.java:88)
    at org.junit.Assert.failNotSame(Assert.java:828)
    at org.junit.Assert.assertSame(Assert.java:771)
    at org.junit.Assert.assertSame(Assert.java:782)
    at JsonDiff.PickEventDiff.comparejson(PickEventDiff.java:26)
    at JsonDiff.PickEventDiff.main(PickEventDiff.java:32)

It should give me an assertion error, but the test succeeds.

9
  • Did you override equals() and hashCode() for your POJOs? Commented Dec 3, 2021 at 7:30
  • @LHCHIN No I didnt Commented Dec 3, 2021 at 7:32
  • You have to override equals() (generated by your IDE for example) so that you can compare two PickEvents based on their internal details. Please refer to Compare two objects with .equals() and == operator. Commented Dec 3, 2021 at 7:36
  • @LHCHIN – You are right that equals() is needed to compare based on the internal details. But when the method is not overwritten, assertEquals() for two distinct objects should always fail. Why is it not failing here?? Commented Dec 3, 2021 at 7:41
  • 1
    I don't understand why your test is not failing, but be aware Lomboks @Data annotation already provides the equals() and hashCode() methods! Commented Dec 3, 2021 at 7:47

3 Answers 3

2

It should give me an assertion error, but the test succeeds.

Because you use objectMapper.configure(FAIL_ON_UNKNOWN_PROPERTIES, false);. In fact, an exception occurred during the parsing process.

Try:

    public void compareJson() throws Exception {
        final ObjectMapper objectMapper = new ObjectMapper();
        Wrapper wrapper = objectMapper.readValue(new File(""), Wrapper.class);
        Wrapper wrapper2 = objectMapper.readValue(new File(""), Wrapper.class);
        System.out.println(wrapper.equals(wrapper2));
    }

    @Data
    static class Wrapper {
        List<PickEvent> pickEventActivities;
    }
Sign up to request clarification or add additional context in comments.

Comments

0

You are trying to read a PickEvent Object but you're actually sending a list there.

Please change your json to

{
    "pickEventActivities": {
        "orderId": "115",
        "lineNbr": 1,
        "pick": "Hello",
        "activations": [{
            "activationType": "Bi",
            "activationValue": "3"
        }]
    }
}

or try changing your code to

List<PickEvent> list1 = objectMapper.readValue(new File("src/../pickevent1.json"), new ParameterizedTypeReference<PickEvent>(){});

Comments

0

Here is my github repo for Intellij Idea plugin. Basically JSON comparator implemented in java. It compares JSON by fields, values and objects in array

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.