24

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.

weston
  • 54,145
  • 21
  • 145
  • 203
Steve Macculan
  • 2,292
  • 5
  • 22
  • 32
  • possible duplicate of [Detect differences between two json files in c#](http://stackoverflow.com/questions/11034224/detect-differences-between-two-json-files-in-c-sharp) – Ariel Popovsky Jun 09 '15 at 13:05
  • 3
    There's a nice library available here that does exactly that: https://github.com/wbish/jsondiffpatch.net – Faheem Nov 14 '17 at 12:12

5 Answers5

14
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;
    }
}
weston
  • 54,145
  • 21
  • 145
  • 203
8

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);
    }
Dave Slinn
  • 435
  • 6
  • 13
3

I think your best bet is to use JSON.NET to create two JSON objects, then recursively loop through the tree, comparing each node to see if it exists and is equal while you go.

BanksySan
  • 27,362
  • 33
  • 117
  • 216
-1

I think the best way to go here is to create objects using newtonsoft json.http://www.nuget.org/packages/newtonsoft.json/

So, you will have two objects of the same type, which you can easily compare and mark the differences.

Yaroslav Yakovlev
  • 6,303
  • 6
  • 39
  • 59
Pratap Das
  • 504
  • 2
  • 7
  • 20
-1
private IEnumerable<JProperty> JSONCompare(string expectedJSON, string actualJSON)
{
    // convert JSON to object
    JObject xptJson = JObject.Parse(expectedJSON);
    JObject actualJson = JObject.Parse(actualJSON);

    // read properties
    var xptProps = xptJson.Properties().ToList();
    var actProps = actualJson.Properties().ToList();

    // find missing properties
    var missingProps = xptProps.Where(expected => actProps.Where(actual => actual.Name == expected.Name).Count() == 0);

    return missingProps;
}
krlzlx
  • 5,752
  • 14
  • 47
  • 55