10

I used this code to compare two JSON object using Gson in Android:

String json1 = "{\"name\": \"ABC\", \"city\": \"XYZ\"}";
String json2 = "{\"city\": \"XYZ\", \"name\": \"ABC\"}";

JsonParser parser = new JsonParser();
JsonElement t1 = parser.parse(json1);
JsonElement t2 = parser.parse(json2);

boolean match = t2.equals(t1);

Is there any way two get the differences between two objects using Gson in a JSON format?

moffeltje
  • 4,521
  • 4
  • 33
  • 57
Ali
  • 143
  • 1
  • 1
  • 7

1 Answers1

21

If you deserialize the objects as a Map<String, Object>, you can with Guava also, you can use Maps.difference to compare the two resulting maps.

Note that if you care about the order of the elements, Json doesn't preserve order on the fields of Objects, so this method won't show those comparisons.

Here's the way you do it:

public static void main(String[] args) {
  String json1 = "{\"name\":\"ABC\", \"city\":\"XYZ\", \"state\":\"CA\"}";
  String json2 = "{\"city\":\"XYZ\", \"street\":\"123 anyplace\", \"name\":\"ABC\"}";

  Gson g = new Gson();
  Type mapType = new TypeToken<Map<String, Object>>(){}.getType();
  Map<String, Object> firstMap = g.fromJson(json1, mapType);
  Map<String, Object> secondMap = g.fromJson(json2, mapType);
  System.out.println(Maps.difference(firstMap, secondMap));
}

This program outputs:

not equal: only on left={state=CA}: only on right={street=123 anyplace}

Read more here about what information the resulting MapDifference object contains.

Sahil Chhabra
  • 10,621
  • 4
  • 63
  • 62
durron597
  • 31,968
  • 17
  • 99
  • 158
  • As Guava is a big library, if you are only using this from it, you can 'extract' the code to your project, so you don't overload the *Dalvik's 65K method limit* PS: Just keep the license header to tell it is a class/method from Guava Project – Joao Evangelista Sep 01 '15 at 04:33
  • @JoaoEvangelista Guava is fine for Android. See this StackOverflow question: [Is it a good idea to use Google Guava library for Android development?](http://stackoverflow.com/q/14978699/1768232) – durron597 Sep 01 '15 at 04:36
  • OMG. All those blogs... Why I didn't find it early, anyways thanks for the link. Also found in this link a link to *Compatibility Docs* for Android [on Guava](https://code.google.com/p/guava-libraries/wiki/Compatibility#Android) Which says about the use of ProGuard. – Joao Evangelista Sep 01 '15 at 04:41
  • Thanks for your answer. Can you clarify what Maps is in: System.out.println(Maps.difference(firstMap, secondMap)); – Ali Sep 01 '15 at 04:58
  • It's a Guava class, that is a static method. See the link. – durron597 Sep 01 '15 at 05:00
  • 3
    Doesn't this apply only when the Json Object has only primitives? It doesn't seem to work for more nested structures. – Arun Ramakrishnan May 18 '18 at 00:18
  • For this use case, the serialization approach would work, is there a lib which can help in other generic cases? – Saurabh Araiyer Aug 27 '18 at 16:00
  • The implementation is order sensitive for arrays, so it's considering a difference for two arrays element order is different `1,2` and `2,1` String json1="{\"a\":[1,2]}"; String json2="{\"a\":[2,1]}"; affectively, there is no difference between these two most of the time – Davut Gürbüz Oct 11 '21 at 14:21