'Compare two json which has same nested structure and same keys but values can be different?
For example :
Json A =>
{
"customer": {
"age": 29,
"fullName": "Emily Jenkins",
"year":1988,
"address":{
"lineOne":"lineOne",
"lineTwo":"lineTwo"
}
},
"handset":{
"phone":"0123456789",
"colour":"black"
}
}
Json B =>
{
"customer": {
"fullName": "Sammy J",
"age": 31,
"year":1985,
"address":{
"lineTwo":"lineTwo",
"lineOne":"lineOne"
}
},
"handset":{
"colour":"red",
"phone":"0123456788"
}
}
I want compare these two json and it should return true as keys are matching and both json structure is same. So is there a clean way of doing it?
I know using Gson lib I can compare two JsonElement but that would match values as well which I don't want to keep as constraint for my use-case.
JsonParser parser = new JsonParser();
JsonElement p1 = parser.parse(JsonA);
JsonElement p2 = parser.parse(JsonB);
System.out.printf(p1.equals(p2) ? "Equal" : "Not Equal"); //it returns true if values are same.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
