title: Deep Comparing of two json and displaying the differences
body: <p>I am trying to compare two dynamic json data and if they are not equal then i am printing the differences. For this i am using  </p>
  
  <pre><code>Type mapType = new TypeToken&lt;Map&lt;String, Object&gt;&gt;(){}.getType();
  Map&lt;String,Object&gt; firstMap = g.fromJson(jsonElement1, mapType);
  Map&lt;String, Object&gt; secondMap = g.fromJson(jsonElement2, mapType);
  System.out.println(Maps.difference(firstMap, secondMap));
  </code></pre>
  
  <p>I need to display the difference in the console.
  This is the java code in which i am trying to display the differences of the json</p>
  
  <pre><code>      public class Tester {
  public static ArrayList&lt;Object&gt; ls1 = new ArrayList&lt;Object&gt;();
   public static              ArrayList&lt;Object&gt; ls2 = new ArrayList&lt;Object&gt;();
              public static void main(String[] args) throws Exception {
                  JsonParser parser = new JsonParser();
  
                  try{
                      Gson g = new Gson();
                      JsonElement jsonElement1 = parser
                              .parse(new FileReader("D:\\file1.json"));
                      JsonElement jsonElement2 = parser
                              .parse(new FileReader("D:\\file2.json"));
                      System.out.println("Is the two JSON File Same: "+compareJson(jsonElement1, jsonElement2));
                      if(!compareJson(jsonElement1, jsonElement2)){
                          Type mapType = new TypeToken&lt;Map&lt;String, Object&gt;&gt;(){}.getType();
                          Map&lt;String,Object&gt; firstMap = g.fromJson(jsonElement1, mapType);
                          Map&lt;String, Object&gt; secondMap = g.fromJson(jsonElement2, mapType);
                          System.out.println(Maps.difference(firstMap, secondMap));
                      }
                      else{
                          System.out.println("The Two JSON Are SAME!!!!!!!!!!!!!!!");
                      }
  
                  }catch(Exception e1){
                      e1.printStackTrace();
                  }
  
              }
  
              public static boolean compareJson(JsonElement json1, JsonElement json2) {
                  boolean isEqual = true;
  
                  // Check whether both jsonElement are not null
                  if (json1 != null &amp;&amp; json2 != null) {
  
                      // Check whether both jsonElement are objects
                      if (json1.isJsonObject() &amp;&amp; json2.isJsonObject()) {
                          Set&lt;Entry&lt;String, JsonElement&gt;&gt; ens1 = ((JsonObject) json1).entrySet();
                          Set&lt;Entry&lt;String, JsonElement&gt;&gt; ens2 = ((JsonObject) json2).entrySet();
                          JsonObject json2obj = (JsonObject) json2;
                          if (ens1 != null &amp;&amp; ens2 != null &amp;&amp; (ens2.size() == ens1.size())) {
                              // Iterate JSON Elements with Key values
                              for (Entry&lt;String, JsonElement&gt; en : ens1) {
                                  isEqual = isEqual &amp;&amp; compareJson(en.getValue(),json2obj.get(en.getKey()));
                              }
                          } else {
                              return false;
                          }
                      }
                      // Check whether both jsonElement are arrays
                      else if (json1.isJsonArray() &amp;&amp; json2.isJsonArray()) {
                          JsonArray jarr1 = json1.getAsJsonArray();
                          JsonArray jarr2 = json2.getAsJsonArray();
                          if (jarr1.size() != jarr2.size()) {
                              return false;
                          } else {
                              int i = 0;
                              // Iterate JSON Array to JSON Elements
                              for (JsonElement je : jarr1) {
                                  isEqual = isEqual &amp;&amp; compareJson(je, jarr2.get(i));
                                  i++;
                              }
                              if (isEqual) {
                                  Object[] o1 = ls1.toArray();
                                  Object[] o2 = ls2.toArray();
                                  isEqual = Arrays.deepEquals(o1, o2);
                              }
                          }
  
                      }
  
                      // Check whether both jsonElement are null
                      else if (json1.isJsonNull() &amp;&amp; json2.isJsonNull()) {
                          return true;
                      }
  
                      // Check whether both jsonElement are primitives
                      else if (json1.isJsonPrimitive() &amp;&amp; json2.isJsonPrimitive()) {
                          ls1.add(json1);
                          ls2.add(json2);         
                      }               
                  } else if (json1 == null &amp;&amp; json2 == null) {
                      return true;
                  } else {
                      return false;
                  }
                  return isEqual;
              }
          }
  </code></pre>
  
  <p>the object array</p>
  
  <pre><code>if (isEqual) {
                              Object[] o1 = ls1.toArray();
                              Object[] o2 = ls2.toArray();
                              isEqual = Arrays.deepEquals(o1, o2);
                          }
  </code></pre>
  
  <p>the object array o1 and o2 are null dont know why it is null.
  These are the two sample json i am using for testing json1 <code>{"Company List":["Compnay: Paypal","Compnay: eBay","Compnay: Google"]}</code> json2 <code>{"Company List":["Compnay: Viswesh","Compnay: Anand","Compnay: Anand"]}</code> in both the json the value for company is different so the code has to return false but the code return true</p>