If you deserialize objects like Map<String, Object> , you can Guava , you can use Maps.difference to compare two resulting maps.
Note that if you care about the order of the elements, Json does not maintain order in the Object s fields, so this method will not show these comparisons.
Here's how 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 displays:
not equal: only on left={state=CA}: only on right={street=123 anyplace}
Learn more about what information is contained in the MapDifference object.
durron597
source share