How to compare json files applying or rejecting changes

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

All you need is to add GroupDocs.Comparison library to a project and to write just a few lines of code

Adding Comparison library to the project

First thing you need is to add GroupDocs repository to the project

Add GroupDocs repository
Maven
Gradle
Kotlin

Second thing is to add required dependency

The latest version of Comparison API can be found here

Add GroupDocs.Comparison dependency
Maven
Gradle
Kotlin

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
Java
Kotlin

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
Java
Kotlin

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 fileTarget Json fileResult Html fileResult 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

Resultant Json file

Get a Free API License

There are much more options available, for example, sensitivity of comparison, generating summary page and much more. Some of them are available only with valid license. You can get a free temporary license to use the API without evaluation limitations.

Free Online App

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.

See also