### Traditional JSON Verification (for comparison) Source: https://skyscreamer.github.io/JSONassert This example demonstrates the verbose, traditional approach to verifying JSON data, highlighting the complexity JSONassert aims to reduce. ```java JSONObject data = getRESTData("/friends/367.json"); Assert.assertTrue(data.has("friends")); Object friendsObject = data.get("friends"); Assert.assertTrue(friendsObject instanceof JSONArray); JSONArray friends = (JSONArray) friendsObject; Assert.assertEquals(2, data.length()); JSONObject friend1Obj = friends.getJSONObject(data.get(0)); Assert.true(friend1Obj.has("id")); Assert.true(friend1Obj.has("name")); JSONObject friend2Obj = friends.getJSONObject(data.get(1)); Assert.true(friend2Obj.has("id")); Assert.true(friend2Obj.has("name")); if ("Carter Page".equals(friend1Obj.getString("name"))) { Assert.assertEquals(123, friend1Obj.getInt("id")); Assert.assertEquals("Corby Page", friend2Obj.getString("name")); Assert.assertEquals(456, friend2Obj.getInt("id")); } else if ("Corby Page".equals(friend1Obj.getString("name"))) { Assert.assertEquals(456, friend1Obj.getInt("id")); Assert.assertEquals("Carter Page", friend2Obj.getString("name")); Assert.assertEquals(123, friend2Obj.getInt("id")); } else { Assert.fail("Expected either Carter or Corby, Got: " + friend1Obj.getString("name")); } ``` -------------------------------- ### JSONassert Error Message Example Source: https://skyscreamer.github.io/JSONassert This is the output of a JSONassert comparison failure, illustrating how it pinpoints discrepancies in nested JSON structures, like an array element mismatch. ```text friends[id=3].pets[]: Expected bird, but not found ; friends[id=3].pets[]: Contains cat, but not expected ``` -------------------------------- ### Simple JSON Comparison with JSONassert Source: https://skyscreamer.github.io/JSONassert Use JSONAssert.assertEquals for straightforward JSON comparisons. Set the last parameter to false to allow for flexible matching (ignoring order and extra elements), which is recommended for less brittle tests. ```java JSONObject data = getRESTData("/friends/367.json"); String expected = "{friends:[{id:123,name:\"Corby Page\"},{id:456,name:\"Carter Page\"}]}"; JSONAssert.assertEquals(expected, data, false); ``` -------------------------------- ### JSON Comparison with Detailed Error Message Source: https://skyscreamer.github.io/JSONassert When JSONassert encounters a mismatch, it provides a detailed error message indicating the specific location and nature of the difference, such as expected vs. actual values in an array. ```java String expected = "{id:1,name:\"Joe\",friends:[{id:2,name:\"Pat\",pets:[\"dog\"]},{id:3,name:\"Sue\",pets:[\"bird\",\"fish\"]}],pets:[]}"; String actual = "{id:1,name:\"Joe\",friends:[{id:2,name:\"Pat\",pets:[\"dog\"]},{id:3,name:\"Sue\",pets:[\"cat\",\"fish\"]}],pets:[]}" JSONAssert.assertEquals(expected, actual, false); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.