How to compare two json objects using testng

2.5k Views Asked by At

Here I am having 2 JSONObjects with same data as

{
  "userId": 1,
  "id": 1,
  "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
  "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
}

I want to comapre 2 objects using TestNG.

I tried in below way but it is giving wrong results.

Assert.assertEquals(actualObject, ExpectedObject);

Can any one help me on this ?

1

There are 1 best solutions below

2
On BEST ANSWER

I'd use JSONAssert - https://github.com/skyscreamer/JSONassert

JSONObject data = getRESTData("/friends/367.json");
String expected = "{friends:[{id:123,name:\"Corby Page\"},{id:456,name:\"Carter Page\"}]}";
JSONAssert.assertEquals(expected, data, false);