As per business need I have to compare JSON response of API URL , which suppose to be a generic solution. JSON compare needs to be include value mismatch as well as schema mismatch too.
As per the requirement is to be a generic solution. I can not create a java object as per the response and compare as response JSON can be anything with any schema ,so JSON to object convention is not possible as object structure can not be defined before hand and will change with each sets of API.
What I tried : Try to implement
Approach 1) flipkart-incubator/zjsonpatch: This is an implementation of RFC 6902 JSON Patch written in Java (github.com)
Approach 2) https://github.com/wnameless/json-flattener
But with this implementation problem is that
but Main issue is that , all is doing case to case matching and able to handle mismatch in sequence in response. Suppose API response of two API as follows for which I want to compare….
Response of API 1
{
"name": "name1",
"address": "",
"skillset": [
{
"lang": "java",
"projectName": "project1"
},
{
"lang": "c ",
"projectName": "project2"
}
]
}
Response of API 2 :
{
"name": "name1",
"address": "",
"skillset": [
{
"lang": "c ",
"projectName": "project2"
},
{
"lang": "java",
"projectName": "project1"
}
]
}
In this two JSON response , array element are same but they not matching index wise in skillset array. My requirement is like this as for name "name1", as skillset is same (just the response order is different), I have to consider this as "no mismatch" in JSON response.
But the above two implementation showing it as mismatch in JSON response and in skill set array , sequence is different in both the response.
May be this can be easily done by json to java object conversion and then do the object compare. But as this is a generic solution and JSON response schema is not known beforehand so its not possible to create any object structure.
Any one can suggest any approach , tool or process through which this can be achieve? Mainly I need to do a json comparation irrespective of JSON array sequence.
CodePudding user response:
I found something similar to your requirement Not 100% sure but maybe this could help
You need your mapper to read the tree Assuming you are using Jackson
You can have
ObjectMapper mapper=new ObjectMapper();
JsonNode object1=mapper.readTree(response1):
JsonNode object2=mapper.readTree(response2);
assertEquals(object1, object2);
Even though the order is not the same, it should give you true.
Click here for more
CodePudding user response:
https://github.com/octomix/josson https://mvnrepository.com/artifact/com.octomix.josson/josson
This new library can do the job.
implementation 'com.octomix.josson:josson:1.3.20'
------------------------------------------------
ObjectMapper mapper=new ObjectMapper();
JsonNode api1 = mapper.readTree("{\n"
"\"name\": \"name12\",\n"
"\"address\": \"\",\n"
"\"skillset\": [\n"
" {\n"
" \"lang\": \"java\",\n"
" \"projectName\": \"project1\"\n"
" },\n"
" {\n"
" \"lang\": \"c \",\n"
" \"projectName\": \"project2\"\n"
" }\n"
"]\n"
"}");
JsonNode api2 = mapper.readTree("{\n"
"\"name\": \"name12\",\n"
"\"address\": \"\",\n"
"\"skillset\": [\n"
" \n"
" {\n"
" \"lang\": \"c \",\n"
" \"projectName\": \"project2\"\n"
" },\n"
" {\n"
" \"lang\": \"java\",\n"
" \"projectName\": \"project1\"\n"
" }\n"
"]\n"
"}");
System.out.println(api1.equals(api2)); // -> false
Jossons jossons = new Jossons();
jossons.putDataset("api1", Josson.create(api1));
jossons.putDataset("api2", Josson.create(api2));
System.out.println(jossons.evaluateQuery("api1 = api2")); // -> true