Issue
How can I compare two JSON, one I get from the database and the second One from UI (means when I click on the edit button)? How can I compare these two JSON?
First Json
{
"id": 2,
"createdAt": "07-01-2021 15:26:16",
"updatedAt": "07-01-2021 15:26:16",
"offerAcceptedDate": "06-30-2021 07:14:00",
"offerAmount": 100000,
"offerDate": "06-30-2021 07:14:00",
"offerFile": "string",
"possibleCloseDate": "06-30-2021 07:14:00",
"remarks": "string",
"salesId": 8,
"status": "Active",
"contactId": 32,
"createdById": 1,
"offerAcceptedById": 1
}
2nd JSON
{
"createdAt": "09-01-2021 15:26:16",
"updatedAt": "10-01-2021 15:26:16",
"offerAcceptedDate": "06-30-2021 07:14:00",
"offerAmount": 500000,
"offerDate": "06-30-2021 07:14:00",
"offerFile": "string",
"possibleCloseDate": "06-30-2021 07:14:00",
"remarks": "string",
"salesId": 8,
"status": "Active",
"contactId": 32,
"createdById": 1,
"offerAcceptedById": 1
}
Also, compare these two JSON and also show activity what column" changed. Like this:
createdAt: Changed
updatedAt: Changed
offeramount: Changed
Solution
1)First of all I get data from DB using findById(JPA Repository)
2)Compare Db field and request body field
like that :
LeadOffer leadOffer = leadOfferRepository.findById(offerId).orElse(null);
JsonObject jsonObject = new JsonObject();
JsonArray jsonArray= new JsonArray();
if (leadOffer.getAmount() != leadOfferRequest.getAmount())
{
offer.setAmount(leadOfferRequest.getAmount());
jsonObject.addProperty("amount",leadOfferRequest.getAmount() );
jsonObject1.addProperty("amount",leadOffer.getAmount() );
}
jsonArray.add(jsonObject);
Answered By - SYED MUSTAFA HUSSAIN