pravat231
pravat231

Reputation: 780

Better way of comparing two JSON data in Automation Script

I am new to JBehave Rest automation script. Below I wrote few lines of code where my requirement is to compare each and every actual JSON field data with the expected data. Here the number of fields in JSON is huge how can i write the script in best way which will handle such scenario.

JSONObject actualjson = new JSONObject(actualJsonresponse);
JSONArray actualjsonData = actualjson.getJSONArray("outputDtlList");
JSONObject expectedjson = new JSONObject(RTRestServicesBean.getConfigurationJsonConfigValue());
JSONArray expectedjsonData = expectedjson.getJSONArray("outputDtlList");

String actual_storagetype = actualjsonData.getJSONObject(0).getString("storageType");
String expected_storagetype = expectedjsonData.getJSONObject(0).getString("storageType");

Assert.assertEquals(actual_storagetype, expected_storagetype);

String actual_locnNbr = actualjsonData.getJSONObject(0).getString("locnNbr");
String expected_locnNbr = expectedjsonData.getJSONObject(0).getString("locnNbr");

Assert.assertEquals(actual_locnNbr, expected_locnNbr);

Upvotes: 2

Views: 2139

Answers (2)

Peter Thomas
Peter Thomas

Reputation: 58058

Consider switching to Karate, it handles full JSON payload comparison much better:

* def json = { foo: 'world', hey: 'ho', zee: [1, 2, 3] }
* remove json.hey
* match json == { foo: 'world', zee: [1, 2, 3] }

Upvotes: 0

VaL
VaL

Reputation: 1167

Consider using net.javacrumbs.json-unit:json-unit.

import static net.javacrumbs.jsonunit.JsonAssert.*;

...

assertJsonEquals(expectedjson, actualjsonData);

Upvotes: 1

Related Questions