
Show HN: JSON.equals in Java to compare two JSON's - sanketsarang
https://tech.blobcity.com/2018/09/02/json-equals-in-java-to-compare-two-jsons/
======
jermo
Thought I'd share something I discovered about Json comparison. This simple
line also works:

    
    
      return jsonObject1.toMap().equals(jsonObject2.equals);
    

because JsonObject uses HashMap internally which supports equals(). The
downside is it would be slower because toMap() creates a copy.

Btw, downloaded java-commons as Maven dependency from JitPack:
[https://jitpack.io/#blobcity/java-commons](https://jitpack.io/#blobcity/java-
commons)

~~~
sanketsarang
Yes this also works. We were using this for a while until we landed up with
very large JSON's. .toMap() is very expensive and takes significantly longer
to execute on large JSON's.

------
mscasts
Can't one just simply check if the strings are equal?

~~~
sanketsarang
No string compare does not work, as the order of elements may not be the same
so the strings may not be the same. The utility will report both JSON's as
equal even if order of elements is different.

~~~
mscasts
Ok cool, thanks for the explanation!

