Questions tagged [jsonassert]

JSONAssert is a Java library for simplifying JUnit tests which need to compare complex JSON structures.

JSONAssert is an open-source library aimed at simplifying JUnit tests which need to compare complex JSON structures.

The tests contain simple assertions which resemble string comparisons, e.g.:

final String actual = ...; // get JSON string from a REST endpoint
final String expected = 
    "{success:true, items: [{id:4711, name:\"Cat\"}, {id:1147,name:\"Squirrel\"}]}";
JSONAssert.assertEquals(expected, actual, false);

The library parses the strings into JSON objects and compares their logical structures.

JSONAssert can also compare org.json objects.

39 questions
1
vote
1 answer

JSONAssert fails due to order mismatch even with JSONCompareMode=LENIENT

I'm using the JSONAssert library to compare two JSON arrays. The arrays to be compared look like this: actual {"components": [ { "createdAt": "2022-03-03T10:22:01.959148348Z", "createdBy": "abc", …
Akshay Maldhure
  • 787
  • 2
  • 19
  • 38
1
vote
0 answers

JSONAssert.assertequals to compare two jsons fails if the resultant payload has additional array value

My api has a response payload that is inside an array and the size of the array can change between test runs. Array is big hence not posting the expected / actual values here. I have assert code that can compare two payload and ignore values that…
John Smith
  • 11
  • 2
1
vote
1 answer

JSONAssert ignore order for specific field

There are two JSON strings to be compared by JSONAssert: StringA { "items": [ "SfWn8eQ", "QiOiJrw", "2Npc2Nv" ], "auths": [ "5895c0a1-0fa9-4222-bbfb-5f96f6737cd7", …
yasi
  • 451
  • 7
  • 18
1
vote
1 answer

JSONAssert - Compare within a specified range

I am comparing JSON responses from two different servers. They should basically match but due to caching, there are some small differences in fields such a temperature, wind speed, etc. I am currently using a customization to ignore certain fields…
M Thurman
  • 41
  • 1
  • 7
1
vote
1 answer

Assert json parameter value in second response with respect to same first request

App is related to an f&b business, I have following scenarios (api calls): Create Order - on creating an order of any food item (let it be X), inventory of which is updated at back-end (this value is under test, let it be P1) Get Inventory - (this…
Abhinav
  • 51
  • 9
1
vote
2 answers

Using JSONAssert to check if an item exists in a JSON array

I have a JSONObject that is similar to something like this: { "category":"abc" "staus":""open" "external":[ {"name":"123", "type":"OTHER"}, {"name":"678", "type":"ALPHA"}, {"name":"890", "type":"DELTA"} ] } If I want to use…
Wonderjimmy
  • 499
  • 1
  • 6
  • 15
1
vote
1 answer

Validate JSON String using REGEX in JSONAssert java

I am storing my expected json string in the json file under resources as shown below. The json string consists of regular expression. I am using JSONAssert Library to compare two json strings. { "timestamp":…
vkrams
  • 7,267
  • 17
  • 79
  • 129
1
vote
2 answers

java org.json compare object

I'm using org.json to convert XML to JSON and I want to unit test (using JUnit) my parsing process. However, when I try to compare the objects using json.equals it fails. I understand that jackson has implemented the equals method properly. Is there…
itaied
  • 6,827
  • 13
  • 51
  • 86
1
vote
1 answer

JSONAssert.assertEquals(objActual, jObject, true) is not working as expected

I need to assert two JSON Objects, for that I am using JSONAssert jar. import org.skyscreamer.jsonassert.JSONAssert; . .//some code . JSONAssert.assertEquals(obj1, jObject, true); When the control reaches JSONAssert line, the execution just…
user3712016
  • 203
  • 3
  • 6
  • 16
1
vote
1 answer

JSON format results from HTTPBuilder GET changed between 0.5.2 and 0.6

We recently updated dependencies of a REST integration test which uses Groovy, HTTPBuilder, JSONAssert and Junit. When we went from HTTPBuilder 0.5.2 to 0.6 many of our tests failed. We found that the format of the response changed due to a new…
Robert
  • 675
  • 8
  • 16
0
votes
0 answers

Combine ResultActions andExpect jsonPath value with JSONCompareMode

I test my json api like that: void testJsonCompareMode() throws Exception { mockMvc.perform(MockMvcRequestBuilders.post("/api/test") .content("{\"hello\":\"world\"}") …
u4963840
  • 81
  • 1
  • 8
0
votes
0 answers

Ignore specific node within array when the element inside the node has a specific value while comparing two JSON in Java

I am trying to compare two JSON structures using Custom comparator of JSONAssert package. For example, Data1: { "name": "John", "details": { "age": 30, "State": { "City": "Buffalo", "Country" :[ { …
0
votes
0 answers

Comparing 2 complex nested array of Json objects while ignoring one field that is "documentURL" in Java and getting error " class has no field" error

error image that show value is not getting populated from jsonBelow given is json file used for comparison assume It is having 2 copies which are getting compared { "FileInformation": [ { "ProductCode": "", "MaterialName": "", …
0
votes
1 answer

How JSONAssert will treat int 5 and double 5.0 same

I have two JSON objects represented as Java Strings Expected: { "id" : "1234567" "balance" : "123" } I converted this into JSON object and trying to validate this with output JSON Actual: { "id" : "1234567" "balance" : "123.00" } I am using…
0
votes
1 answer

JSONCompareMode.NON_EXTENSIBLE is ignored when comparing arrays of objects and ignoring a field in this array

I'm using JSONAssert 1.5.0 to compare Jsons. I have a custom comparator to ignore ID fields, which would differ from testrun to testrun. JSON would look like this { "id": 34, "fieldxy": abc "status": [ { "id": 69, "code":…
Krwasch
  • 29
  • 1
  • 7