1

Is there a built in library or code that can help me compare two json arrays semantically:

I am running unit test and have to compare two json arrays and see if they are equal

Json Array1=

[{"job_name":"CE","role_name":"Excellence"},
{"job_name":"PA","role_name":"Approver"},
{"job_name":"RO","role_name":"Financial"},
{"job_name":"RP","role_name":"Publisher"},
{"job_name":"SA","role_name":"Analyst"},
{"job_name":"TS","role_name":"Supervisor"},
{"job_name":"TT","role_name":"test"},
{"job_name":"ZG","role_name":"Manager"},
{"job_name":"ZI","role_name":"Manager"},
{"job_name":"ZJ","role_name":"Manager"}]

Json Array2:

{"role_name":"Excellence","job_name":"CE"},
{"role_name":"Approver","job_name":"PA"},
{"role_name":"Financial","job_name":"RO"},
{"role_name":"Publisher","job_name":"RP"},
{"role_name":"Analyst","job_name":"SA"},
{"role_name":"Supervisor","job_name":"TS"},
{"role_name":"test","job_name":"TT"},
{"role_name":"Manager","job_name":"ZG"},
{"role_name":"Manager","job_name":"ZI"},
{"role_name":"Manager","job_name":"ZJ"}

When I compare this using http://www.jsondiff.com/ , it returns that two files were semantically identical.

For the Java code, I tried using the following two libraries:

JSONCompareResult result = JSONCompare.compareJSON(Array1, Array1, 
JSONCompareMode.STRICT) :

JSONCompareResult result = JSONCompare.compareJSON(Array1, Array1, 
JSONCompareMode.LENIENT) :

Also, I tried using the JSON assert: JSONAssert.assertEquals(Array1, Array1, false);

But in both the cases, the libraries flag the JSON arrays as different.

Any help or suggestion would be much appreciated.

  • 5
    Possible duplicate of [Compare two JSON objects in Java](https://stackoverflow.com/questions/2253750/compare-two-json-objects-in-java) – Gino Mempin Mar 16 '18 at 00:07

2 Answers2

0

This is probably long winded for what you want, I don't know any short cut, so I tend to try and work around it. You would need to check the arrays are equal length, ordered the same etc... But it may help someone sometime..

import org.json.JSONArray;
import org.json.JSONObject;

public class ClassCompareTwoJsonArrays {

private static String JSONArray1 = "[{\"job_name\":\"CE\",\"role_name\":\"Excellence\"},"
        + "{\"job_name\":\"PA\",\"role_name\":\"Approver\"},"
        + "{\"job_name\":\"RO\",\"role_name\":\"Financial\"},"
        + "{\"job_name\":\"RP\",\"role_name\":\"Publisher\"},"
        + "{\"job_name\":\"SA\",\"role_name\":\"Analyst\"},"
        + "{\"job_name\":\"TS\",\"role_name\":\"Supervisor\"},"
        + "{\"job_name\":\"TT\",\"role_name\":\"Test1\"},"
        + "{\"job_name\":\"ZG\",\"role_name\":\"Manager\"},"
        + "{\"job_name\":\"ZI\",\"role_name\":\"Manager\"},"
        + "{\"job_name\":\"ZJ\",\"role_name\":\"Manager\"}]";

private static String JSONArray2 = "{\"role_name\":\"Excellence\",\"job_name\":\"CE\"},"
        + "{\"role_name\":\"Approver\",\"job_name\":\"PA\"},"
        + "{\"role_name\":\"Financial\",\"job_name\":\"RO\"},"
        + "{\"role_name\":\"Publisher\",\"job_name\":\"RP\"},"
        + "{\"role_name\":\"Analyst\",\"job_name\":\"SA\"},"
        + "{\"role_name\":\"Supervisor\",\"job_name\":\"TS\"},"
        + "{\"role_name\":\"Test1\",\"job_name\":\"TT\"},"
        + "{\"role_name\":\"Manager\",\"job_name\":\"ZG\"},"
        + "{\"role_name\":\"Manager\",\"job_name\":\"ZI\"},"
        + "{\"role_name\":\"Manager\",\"job_name\":\"ZJ\"}";


public static void main(String[] args) {

    //Array1 is set out like an array enclosed in []
    //Array2 is not so...
    JSONArray2 = "[" +JSONArray2 +"]";


    //Modify Array1 to allow JSON to read array by giving the array a name
    //Possibly an unneccesary work around - Self taught...
    JSONArray1 = "{Test1:{JSONArray1:" +JSONArray1 +"}}";
    JSONObject objAll =  new JSONObject(JSONArray1); 
    JSONObject objTest1 = objAll.getJSONObject("Test1");
    JSONArray arrJSONArray1 = objTest1.getJSONArray("JSONArray1");


    //Modify Array1 to allow JSON to read array by giving the array a name
    //Possibly an unneccesary work around - Self taught...
    JSONArray2 = "{Test2:{JSONArray2:" +JSONArray2 +"}}";
    JSONObject objAll2 =  new JSONObject(JSONArray2); 
    JSONObject objTest2 = objAll2.getJSONObject("Test2");
    JSONArray arrJSONArray2 = objTest2.getJSONArray("JSONArray2");


    // Initilise counts for record check results
    int FoundTheSame = 0;
    int FoundDifferent = 0;


    //Read Arrays and compare - code assumes arrays are the same length and order of records
    for (int i = 0; i < arrJSONArray1.length(); ++i) 
    {

        //Read data from Array1 for comparison
        JSONObject objTestJSONArray1 = arrJSONArray1.getJSONObject(i);
        String JSONArray1Job = objTestJSONArray1.getString("job_name");
        String JSONArray1Role = objTestJSONArray1.getString("role_name");

        //Read data from Array2 for comparison
        JSONObject objTestJSONArray2 = arrJSONArray2.getJSONObject(i);
        String JSONArray2Job = objTestJSONArray2.getString("job_name");
        String JSONArray2Role = objTestJSONArray2.getString("role_name");


        //Compare arrays and show results
        if (JSONArray1Job.equals(JSONArray2Job) && JSONArray1Role.equals(JSONArray2Role))
        {  // The same
            FoundTheSame = FoundTheSame + 1;
        } else {
            //Different
            FoundDifferent = FoundDifferent + 1;
            System.out.println("Error" + " " +FoundDifferent +", "
            +"Array Index (starts at 0) = " +i +", Array1 = " +JSONArray1Job +" "
            + JSONArray1Role +", Array2 = " +JSONArray2Job +", " + JSONArray2Role);

        }//end if
    }//end for

    System.out.println("Records found to be the same = " +FoundTheSame + " of " +arrJSONArray1.length());

}//end main
}//end class

Which results in the following when they are the same - as with the data you provided...

Records found to be the same = 10 of 10

Or if I changed something to highlight errors, an example response is...

Error 1, Array Index (starts at 0) = 4, Array1 = SA Analyst, Array2 = SA, Analystt
Error 2, Array Index (starts at 0) = 8, Array1 = AA Manager, Array2 = ZI, Manager
Records found to be the same = 8 of 10
Nig
  • 1
  • 2
0

I suggest you to use https://github.com/eBay/json-comparison library here is the code that test both are semantically identical ( I just took the first 2 lines in array) BTW with this library you can also ignore specific path/ filter paths / order with really friendly output check it out

  String array1 = "[{\"job_name\":\"CE\",\"role_name\":\"Excellence\"},\n" +
            "{\"job_name\":\"PA\",\"role_name\":\"Approver\"}]";

    String array2 = "[{\"role_name\":\"Excellence\",\"job_name\":\"CE\"},\n" +
            "{\"role_name\":\"Approver\",\"job_name\":\"PA\"}]";

    JsonComparisonResult compareResult = JsonCompare.builder().build()
            .compare(array2, array1);

    assertTrue(compareResult.isMatch());
MedoCode
  • 1
  • 1