Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing JSON with arrays

Tags:

java

json

I am trying to compare 2 JSON files, they have arrays with duplicated values.

My first JSON Object has an array like this:

"categories": [
            "May",
            "Apr",
            "Mar"
          ]

My second JSON object has an array like this:

"categories": [
            "May",
            "May",
            "Apr",
            "Apr",
            "Mar",
            "Mar"
          ]

I am comparing the JSON using flat maps that can be found in this link comparing JSONs using guava

Here is part of my code:

 private String smartJSONsCompare(JSONObject leftJson, JSONObject rightJson) {

    String result = "</br>";
    Gson gson = new Gson();
    Type type = new TypeToken<Map<String, Object>>(){}.getType();
    Map<String, Object> leftMap = gson.fromJson(leftJson.toString(), type);
    Map<String, Object> rightMap = gson.fromJson(rightJson.toString(), type);
    Map<String, Object> leftFlatMap = FlatMapUtil.flatten(leftMap);
    Map<String, Object> rightFlatMap = FlatMapUtil.flatten(rightMap);
    MapDifference<String, Object> difference = Maps.difference(leftFlatMap, rightFlatMap);
    StringBuilder SB = new StringBuilder("</br>");
    SB.append("Entries only on LEFT: </br>");
    difference.entriesOnlyOnLeft().forEach((key, value) -> SB.append(key + ": " + value + "</br>"));
    SB.append("Entries only on RIGHT: </br>");
    difference.entriesOnlyOnRight().forEach((key, value) -> SB.append(key + ": " + value + "</br>"));
    SB.append("Entries full difference : </br>");
    difference.entriesDiffering().forEach((key, value) -> SB.append(key + ": " + value + "</br>"));
    result = SB.toString();
    return result;
}

I wish to be able to present the difference in a more "smart" way. In other words: showing all the objects / arrays in the JONSs that don't match. What is missing or what was added to the compared JSON.

For the "categories" array my code returns a message that their is a mismatch, but doesn't state the difference in an elegant way.

What can I do?

like image 822
Tal Angel Avatar asked Mar 07 '19 14:03

Tal Angel


People also ask

How do you compare two JSON objects?

Comparing Json: Comparing json is quite simple, we can use '==' operator, Note: '==' and 'is' operator are not same, '==' operator is use to check equality of values , whereas 'is' operator is used to check reference equality, hence one should use '==' operator, 'is' operator will not give expected result.

What is the difference between an array and JSON?

JSON Syntax JSON defines only two data structures: objects and arrays. An object is a set of name-value pairs, and an array is a list of values. JSON defines seven value types: string, number, object, array, true, false, and null.

Can you use arrays in JSON?

Arrays in JSON are almost the same as arrays in JavaScript. In JSON, array values must be of type string, number, object, array, boolean or null. In JavaScript, array values can be all of the above, plus any other valid JavaScript expression, including functions, dates, and undefined.

Can we compare two JSON files?

You can also directly compare two JSON files by specifying their urls in the GET parameters url1 and url2. Then you can visualize the differences between the two JSON documents. It highlights the elements which are different: Different value between the two JSON: highlight in red color.


3 Answers

I have change a bit in your solution to get the wanted result.

I would do my difference check in List, therefore I will create method to change JSON to list of strings based on your code:

private static List<String> jsonToList(String json){
    List<String> list = new ArrayList<>();
    Gson gson = new Gson();
    Type type = new TypeToken<Map<String, Object>>(){}.getType();
    Map<String, Object> jsonMap = gson.fromJson(json, type);
    Map<String, Object> flatten = FlatMapUtil.flatten(jsonMap);
    flatten.forEach((k, v) -> list.add(v.toString()));
    return list;
}

Update
When I answered the question I did things a bit fast, the jsonToList was based on your code. As it is right now it is over complicated to what you are asking for. I have therefore made much lighter version using the following method in stead:

private static List<String> jsonToList(String json) {
    JSONObject response = new JSONObject(json);
    List<String> list = new ArrayList<>();
    JSONArray jsonArray = response.getJSONArray("categories");
    if (jsonArray != null) {
        for (int i = 0; i < jsonArray.length(); i++) {
            list.add(jsonArray.get(i).toString());
        }
    }
    return list;
}

That said, now you have two choices and it is up to you to find out which one fits best to your needs and take it from here.

End of Update


for this example I have made 3 test examples

String main = "{\"categories\":[\"May\",\"Apr\",\"Mar\"]}";
String json1 = "{\"categories\":[\"May\",\"May\",\"Apr\",\"Apr\",\"Mar\",\"Mar\"]}";
String json2 = "{\"categories\":[\"May\",\"Apr\",\"Apr\",\"Mar\",\"Mar\",\"Mar\"]}";
String json3 = "{\"categories\":[\"May\",\"Apr\",\"Mar\",\"Mar\"]}";

in my second step I will create a

List<String> mainList = jsonToList(main);
List<String> list1 = jsonToList(json1);

so far so good. Now I make a method to take the extra difference of the 2 list, that mean as you requested in your comments, we take only all values that are duplicated more than once and return them in list. In this method I used hashmap only count duplicates and than take the all that is repeated more than 1 time:

private static List<String> diffList(List<String> mainList, List<String> secondList){
    List<String> list = new ArrayList<String>();
    Map<String, Integer> wordCount = new HashMap<>();
    for(String word: secondList) {
        if(mainList.contains(word)) {
            Integer count = wordCount.get(word);
            wordCount.put(word, (count == null) ? 1 : count + 1);
            if(wordCount.get(word) > 1){
                list.add(word);
            }
        }
    }
    return list;
}

Finally I would test all cases, for instance for list1:

List<String> diff1 = diffList(mainList, list1);
for (String s : diff1) {
    System.out.println(s);
}

The output will be

May
Apr
Mar

for list2

Apr
Mar
Mar

And for list3

Mar

Now I will separate view method from the your method and create some thing like, just to make my code more clear and easy to work with:

private static String viewResult(List<String> list1, List<String> list2, List<String> duplicate){
    String result;
    StringBuilder SB = new StringBuilder("</br>");
    SB.append("Entries only on LEFT: </br>");
    list1.forEach(e -> SB.append(e + "</br>"));
    SB.append("Entries only on RIGHT: </br>");
    list2.forEach(e -> SB.append(e + "</br>"));
    SB.append("Entries full difference : </br>");
    duplicate.forEach(e -> SB.append(e + "</br>"));
    result = SB.toString();
    return result;
}

So if we put all this code together I will be some thing like this, and the following code is to demonstrate how things works, but from here you can take it to the next level in your code:

public static void main(String[] args) {
    String main = "{\"categories\":[\"May\",\"Apr\",\"Mar\"]}";
    String json1 = "{\"categories\":[\"May\",\"May\",\"Apr\",\"Apr\",\"Mar\",\"Mar\"]}";
    String json2 = "{\"categories\":[\"May\",\"Apr\",\"Apr\",\"Mar\",\"Mar\",\"Mar\"]}";
    String json3 = "{\"categories\":[\"May\",\"Apr\",\"Mar\",\"Mar\"]}";

    List<String> mainList = jsonToList(main);

    List<String> list1 = jsonToList(json1);
    List<String> diff1 = diffList(mainList, list1);
    for (String s : diff1) {
        System.out.println(s);
    }

    String view = viewResult(mainList, list1, diff1);
}

private static List<String> jsonToList(String json){
    List<String> list = new ArrayList<String>();
    Gson gson = new Gson();
    Type type = new TypeToken<Map<String, Object>>(){}.getType();
    Map<String, Object> jsonMap = gson.fromJson(json, type);
    Map<String, Object> flatten = FlatMapUtil.flatten(jsonMap);
    flatten.forEach((k, v) -> list.add(v.toString()));
    return list;
}

private static List<String> diffList(List<String> mainList, List<String> secondList){
    List<String> list = new ArrayList<String>();
    Map<String, Integer> wordCount = new HashMap<>();
    for(String word: secondList) {
        if(mainList.contains(word)) {
            Integer count = wordCount.get(word);
            wordCount.put(word, (count == null) ? 1 : count + 1);
            if(wordCount.get(word) > 1){
                list.add(word);
            }
        }
    }
    return list;
}

private static String viewResult(List<String> list1, List<String> list2, List<String> duplicate){
    String result;
    StringBuilder SB = new StringBuilder("</br>");
    SB.append("Entries only on LEFT: </br>");
    list1.forEach(e -> SB.append(e + "</br>"));
    SB.append("Entries only on RIGHT: </br>");
    list2.forEach(e -> SB.append(e + "</br>"));
    SB.append("Entries full difference : </br>");
    duplicate.forEach(e -> SB.append(e + "</br>"));
    result = SB.toString();
    return result;
}
like image 142
Maytham Avatar answered Oct 20 '22 01:10

Maytham


If you want something more generic with a good diff you could utilize AssertJ here. Its usually used for Testing, but the diff looks really good and you can also use it in normal code.

Example:

Expecting:
  <["Mai", "Apr", "Mar"]>
to contain exactly in any order:
  <["May", "Apr", "Mar", "Mar"]>
elements not found:
  <["May", "Mar"]>
and elements not expected:
  <["Mai"]>

Can be created by:

[...]
import org.assertj.core.api.Assertions;

public class JsonTest {
    final static String arr = " [\n"+
            "            \"Mai\",\n"+
            "            \"Apr\",\n"+
            "            \"Mar\"\n"+
            "          ]";
    final static String arr2 = " [\n"+
            "            \"May\",\n"+
            "            \"Apr\",\n"+
            "            \"Mar\",\n"+
            "            \"Mar\"\n"+
            "          ]";

    public static void main(String[] args){
        System.out.println(smartJSONsCompare(arr,arr2));
    }

    private static String smartJSONsCompare(String leftJson, String rightJson) {
        Gson gson = new Gson();
        Type type = new TypeToken<List<String>>(){}.getType();
        List<String> left = gson.fromJson(leftJson, type);
        List<String> right = gson.fromJson(rightJson, type);
        try{
            Assertions.assertThat(left).containsExactlyInAnyOrderElementsOf(right);
        }catch(AssertionError  ae){
            return ae.getMessage();
        }
        return "Matched";
    }
  }

I added the dependencies in gradle with:

dependencies {
    compile("org.assertj:assertj-core:3.11.1")
}
like image 22
thi gg Avatar answered Oct 20 '22 02:10

thi gg


If you want to create a patch between your two JSON Objects have a look at json-patch.

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.fge.jsonpatch.diff.JsonDiff;
import java.io.IOException;

public class JsonPatchTest {
    public static void main(String[] args) throws IOException {
        String jsonFirst = "{\"categories\":[\"May\",\"Apr\",\"Mar\"]}";
        String jsonSecond = "{\"categories\":[\"May\",\"May\",\"Apr\",\"Apr\",\"Mar\",\"Mar\"]}";

        ObjectMapper mapper = new ObjectMapper();
        JsonNode jsonNodeFirst = mapper.readTree(jsonFirst);
        JsonNode jsonNodeSecond = mapper.readTree(jsonSecond);

        JsonNode patchNode = JsonDiff.asJson(jsonNodeFirst, jsonNodeSecond);

        System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(patchNode));
    }
}

Would produce the following output for your scenario:

[ {
  "op" : "replace",
  "path" : "/categories/1",
  "value" : "May"
}, {
  "op" : "replace",
  "path" : "/categories/2",
  "value" : "Apr"
}, {
  "op" : "add",
  "path" : "/categories/-",
  "value" : "Apr"
}, {
  "op" : "add",
  "path" : "/categories/-",
  "value" : "Mar"
}, {
  "op" : "add",
  "path" : "/categories/-",
  "value" : "Mar"
} ]
like image 44
FlorianDe Avatar answered Oct 20 '22 01:10

FlorianDe