Are there any libraries in .Net to help compare and find differences between two json objects? I've found some solutions available for JavaScript, but nothing interesting for C#. The point of my question is to create json with changes marked in some way, based on the comparison. So that the user could see where the changes are.
It's possible to use a recursive function that iterates by the object keys. Then use the Object.is to test for NaN and null . Then test if the second object is the type that cast to false like 0 , NaN , or null . List the keys of both objects and concatenate them to test of missing keys in the obj1 and then iterate it.
You can also directly compare two JSON files by specifying their urls in the GET parameters url1 and url2. Then you can visualize the differences between the two JSON documents. It highlights the elements which are different: Different value between the two JSON: highlight in red color.
A diff takes two JSON objects and presents any differences between them. Diff has several uses. A key use is displaying a clear summary of differences between large objects, enhancing the visibility of changes. This enables manual, user-interface assisted, or client actions to resolve differences.
using Microsoft.XmlDiffPatch; using Newtonsoft.Json;
Convert each json to xml and use MS XmlDiff libary. Available on nuget. Differences are given in another xml doc which here I write to the console. This is suitable for unit testing for example.
public bool CompareJson(string expected, string actual) { var expectedDoc = JsonConvert.DeserializeXmlNode(expected, "root"); var actualDoc = JsonConvert.DeserializeXmlNode(actual, "root"); var diff = new XmlDiff(XmlDiffOptions.IgnoreWhitespace | XmlDiffOptions.IgnoreChildOrder); using (var ms = new MemoryStream()) using (var writer = new XmlTextWriter(ms, Encoding.UTF8)) { var result = diff.Compare(expectedDoc, actualDoc, writer); if (!result) { ms.Seek(0, SeekOrigin.Begin); Console.WriteLine(new StreamReader(ms).ReadToEnd()); } return result; } }
I have used different JSON objects than those in your example but it will apply to your case correctly.
private static string GetJsonDiff(string action, string existing, string modified, string objectType) { // convert JSON to object JObject xptJson = JObject.Parse(modified); JObject actualJson = JObject.Parse(existing); // read properties var xptProps = xptJson.Properties().ToList(); var actProps = actualJson.Properties().ToList(); // find differing properties var auditLog = (from existingProp in actProps from modifiedProp in xptProps where modifiedProp.Path.Equals(existingProp.Path) where !modifiedProp.Value.ToString().Equals(existingProp.Value.ToString()) select new AuditLog { Field = existingProp.Path, OldValue = existingProp.Value.ToString(), NewValue = modifiedProp.Value.ToString(), Action = action, ActionBy = GetUserName(), ActionDate = DateTime.UtcNow.ToLongDateString(), ObjectType = objectType }).ToList(); return JsonConvert.SerializeObject(auditLog); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With