Search code examples
c#jsonasp.net-mvcjson-deserializationjsonserializer

compares two JSON-objects and returns a JSON-object with a list of the differences c#


I'm converting View Model into a JSON Object and inserting into the Database.

Now I want to pick any two JSON objects from the database and compare both the JSON objects and see if there is any change in the attribute value and return the list of differences between them. So I'm looking for a function in C# that compares two JSON-objects, and returns a JSON-object with a list of the differences and if possible more data such as coverage metrics. Can someone help me with it.

Example:

var json1 = @"{ ""name"": ""JohnRob"", ""nickname"":""John"", ""age"": 20, ""married"": false }";

var json2 = @"{ ""name"": ""JohnRob"", ""nickname"":""John"", ""age"": 22, ""married"": true }";

Result: The output should be something like

{ "age":[ 20, 22 ], "married":[ false, true ] }


Solution

  • Previous thread here talked about a diff program for this called jsondiffpatch. Looks like it does what you're looking for. It's main object let's you pass a left and right json to diff.

    Format of call is just

    jdp.Patch(left, patch);