Home > Back-end >  Comparing two JSON files using java
Comparing two JSON files using java

Time:12-04

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.

CodePudding user response:

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;
    }

CodePudding user response:

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>(){});
  • Related