How to compare json files applying or rejecting changes
How to compare json files applying or rejecting changes
Leave feedback
On this page
Json files are very popular nowadays. They are used for transferring data on the internet, for configuring applications and so on. In this way, very often people need to compare Json files to see whether they were changed or not. In the same time, it can be very useful to reject one changes and to accept other. In cause you need such functionality, GroupDocs.Comparison is the best solution for you.
How to compare Json files in Java or Kotlin application
First thing you need is to add GroupDocs repository to the project
Add GroupDocs repository
MavenGradleKotlin
Second thing is to add required dependency
The latest version of Comparison API can be found here
Add GroupDocs.Comparison dependency
MavenGradleKotlin
Comparing source and target Json files to see all differences
GroupDocs.Comparison is very powerful library that has lots of options for comparing files, analyzing changes, getting details about each change, accepting or rejecting each change and so on. Here are used just a tiny part of capabilities. More information about them you can find in the documentation
Here is the code you need to compare source and target Json files
JavaKotlin
As a result you will have two files:
.json file with special labels, which shows each change in the document
.html file with human-readable result of comparison
These files can be used to see and analyze changes. But it is not all we want to do, the next step is much more interesting and useful.
Accepting or rejecting changes in Json file
GroupDocs.Comparison library provides not only comparison functionality, but also possibility to get list of changes, reject or accept them and apply to the result document. It can be very useful when some changes are invalid and must be reverted while other are good and must be applied to the document.
Here is the code you need to apply or reject changes in Json file
JavaKotlin
In the code above we are getting list of changes and process each of them. We are analyzing the value of getSourceText() method. In case it contains defaultListTemplate, defaultFileTemplate or useJSP (properties from Json file), the change will be accepted, all other changes will be rejected. To understand what is happening, look images below. After updating, changes are applied with saving as a new file.
The result will be saved as two files
.json valid Json file without changes that were rejected but with changes that were accepted
.html human-readable result file, very similar to result of comparison process, but without rejected changes
Here are screenshots of all files:
Source Json file
Target Json file
Result Html file
Result Html file after applying changes
As you can see, changes pobox -> hacker, 200 -> 88 and 100 -> 6 were rejected, while changes inside properties defaultListTemplate, defaultFileTemplate and useJSP were left in the document. Valid resultant Json document was also saved. You can see it below
Along with full-featured Java/Kotlin library we provide simple, but powerful free applications. You are welcome to compare your DOC or DOCX, XLS or XLSX, PPT or PPTX, PDF, EML, EMLX, MSG and other documents with free to use online GroupDocs Comparison Application.
More details
There are much more examples of using GroupDocs.Comparison with Java/Kotlin available on our GitHub page. You are free to change and use them in order to compare photos, Excel, Word, code files, PowerPoint Presentations and much more supported file formats.
For getting details, options, and examples, you can go through the documentation and GitHub repository. Reach us on the forum for your queries.