Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way of returning differences of two json files programatically

I have two json files and I would like to get a json containing the differences. It is important that only the actual differences of content should be shown, regardless of changing the order of some elements.

What would be the best way to do that? I am searching for a solution as efficient as possible, since jsons may contain lots of data, and users need jobs to be done as quick as possible.

Note: The jsons might contain data encoded at different depths. Any programming language is ok, but I would prefer an answer that could easily be implemented in php.

like image 348
Mihai Bujanca Avatar asked Mar 23 '13 19:03

Mihai Bujanca


People also ask

How can I find the difference between two JSON files?

Comparing Json: Comparing json is quite simple, we can use '==' operator, Note: '==' and 'is' operator are not same, '==' operator is use to check equality of values , whereas 'is' operator is used to check reference equality, hence one should use '==' operator, 'is' operator will not give expected result.

How do you find the difference between two JSON files in node JS?

Solution 1 Your first code step would be to convert the JSON string to an object, using JSON. parse . Then you can use Object. keys to get all keys from the first object, and you can loop over these keys to see the difference in values in the two objects.

How can I find the difference between two JSON files in C#?

var json2 = @"{ ""name"": ""Tod"" }" ; Followed by that, we will call the Diff method on our JsonDiffPatch object. As input, we will pass both JSON strings and as output we will get another JSON string, representing the diff. Note that the order of the JSON parameters influence the order of the diff in the result.


2 Answers

For this task you can try with https://github.com/swaggest/json-diff

It will do key-wise recursive comparison (order of keys does not matter) and produce JSON Patch (specified in RFC 6902 from the IETF).

like image 175
vearutop Avatar answered Sep 28 '22 05:09

vearutop


Basically, what you want is something similar to array_diff_assoc, but applied to json objects, and recursive.

The array_diff functions are not recursive because of reference issues: it is possible to assign a reference of an array to an entry of that array, making the array infinitely recursive. I don't think it is possible to get the same situation with a json object, thus making a recursive function safe.

Let's suppose that you wish to compute the difference between object A and B, and have the result in object C. The principle is to loop over each field of A (a foreach should do), and when:

  • no such field exist in B, copy it over C.
  • a similar field exist in B, put in C the result of the difference of A field with B field, which is a recursive call on the diff function with those field as parameter, as well as a fresh object for the result.

The ordering of A should be respected.

like image 45
didierc Avatar answered Sep 28 '22 04:09

didierc