I have an API which takes some potentially mandatory data to create a temporary session:
e.g.: at first, my
POST /signup
endpoint user need to send me the following data:{ "customer": { "age": 21, "ssn": "000 00 0000", "address": { "street": "Customer St.", "phone": "+66 444 333 222" } } }
Let's call it
JSON a
.
On the other hand, I have some legal partners which requires some of these data, but not all of them:
e.g.:
{ "customer": { "ssn": "The SSN is mandatory to register against XXX Company", "address": { "phone": "XXX Company will send a text message to validate your registration" } } }
Let's call it
JSON b
.
Due to recent legal restrictions, in my information system, I have to keep only mandatory data for the user to carry with his chosen workflow.
Hence my question: is there a function (either built in Jackson or some other JSON handling library or an algorithm you would recommend) I could apply such that given JSON b
and JSON a
, it would output the following JSON:
{
"customer": {
"ssn": "000 00 0000",
"address": {
"phone": "+66 444 333 222"
}
}
}
Thinking a bit, I found something which might be a solution:
- merge
JSON a
withJSON b
, on conflict takeJSON b
values, name the resultJSON c
- make a diff (drop equals data) between
JSON c
andJSON a
, on conflict takeJSON a
values
I know merging two JSON using Jackson can be done using com.fasterxml.jackson.databind.ObjectMapper#readerForUpdating
, so my question could be reduced to: is there a way to make a diff between two JSON and give a conflict resolution function?