16

For example, if I have the following json texts:

 object1{
     field1: value1;
     field2: value2;
     field3: value3;
 }

 object1{
     field1: value1;
     field2: newvalue2;
     field3: value3;
 }

I need something in c# that reads that files and shows the difference. i.e. it can return the following object:

differences {
    object1: { field: field2, old_value: value2, new_value: newvalue2}
}

Is there some API or suggestions to do this?

Tony
  • 10,088
  • 20
  • 85
  • 139
  • Not that i am aware of. But if you write one, post a link to it here. :) – Jesse Chisholm Jun 17 '12 at 16:43
  • I've read the answers above and constructed my own [solution](https://stackoverflow.com/a/53654737/5308054) – Dzmitry Paliakou Dec 06 '18 at 15:36
  • Possible duplicate of [Find and return JSON differences using newtonsoft in C#?](https://stackoverflow.com/questions/24876082/find-and-return-json-differences-using-newtonsoft-in-c) – omajid Dec 06 '18 at 15:46

3 Answers3

5

I recommend you use Weakly-Typed JSON Serialization and write a routine that uses JsonObject like this:

String JsonDifferenceReport(String objectName,
                            JsonObject first,
                            JsonObject second)
{
  if(String.IsNullOrEmpty(objectName))
    throw new ArgumentNullException("objectName");
  if(null==first)
    throw new ArgumentNullException("first");
  if(null==second)
    throw new ArgumentNullException("second");
  List<String> allKeys = new List<String>();
  foreach(String key in first.Keys)
    if (!allKeys.Any(X => X.Equals(key))) allKeys.Add(key);
  foreach(String key in second.Keys)
    if (!allKeys.Any(X => X.Equals(key))) allKeys.Add(key);
  String results = String.Empty;
  foreach(String key in allKeys)
  {
    JsonValue v1 = first[key];
    JsonValue v1 = second[key];
    if (((null==v1) != (null==v2)) || !v1.Equals(v2))
    {
      if(String.IsNullOrEmpty(results))
      {
         results = "differences: {\n";
      }
      results += "\t" + objectName + ": {\n";
      results += "\t\tfield: " + key + ",\n";
      results += "\t\toldvalue: " + (null==v1)? "null" : v1.ToString() + ",\n";
      results += "\t\tnewvalue: " + (null==v2)? "null" : v2.ToString() + "\n";
      results += "\t}\n";
    }
  }
  if(!String.IsNullOrEmpty(results))
  {
    results += "}\n";
  }
  return results;
}

Your choice whether to get reports recursively inside JsonValue v1 and v2, instead of just using their string representation as I did here.

If you wanted to go recursive, it might change the above like this:

  if ((null==v1) || (v1.JsonType == JsonType.JsonPrimitive)
   || (null==v2) || (v2.JsonType == JsonType.JsonPrimitive))
  {
    results += "\t\tfield: " + key + ",\n";
    results += "\t\toldvalue: " + (null==v1) ? "null" : v1.ToString() + ",\n";
    results += "\t\tnewvalue: " + (null==v2) ? "null" : v2.ToString() + "\n";
  }
  else
  {
    results + JsonDifferenceReport(key, v1, v2);
  }

-Jesse

Jesse Chisholm
  • 3,857
  • 1
  • 35
  • 29
1

For some reason, I was not able to use JsonObject in my Web API project. I used JSON.Net and below is my method to get diff. It will give out Array of differences.

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.Equals(existingProp.Value)
            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);
    }
1

I've constructed my own method for Json comparison. It uses Newtonsoft.Json.Linq.

    public static JObject FindDiff(this JToken Current, JToken Model)
    {
        var diff = new JObject();
        if (JToken.DeepEquals(Current, Model)) return diff;

        switch(Current.Type)
        {
            case JTokenType.Object:
                {
                    var current = Current as JObject;
                    var model = Model as JObject;
                    var addedKeys = current.Properties().Select(c => c.Name).Except(model.Properties().Select(c => c.Name));
                    var removedKeys = model.Properties().Select(c => c.Name).Except(current.Properties().Select(c => c.Name));
                    var unchangedKeys = current.Properties().Where(c => JToken.DeepEquals(c.Value, Model[c.Name])).Select(c => c.Name);
                    foreach (var k in addedKeys)
                    {
                        diff[k] = new JObject
                        {
                            ["+"] = Current[k]
                        };
                    }
                    foreach (var k in removedKeys)
                    {
                        diff[k] = new JObject
                        {
                            ["-"] = Model[k]
                        };
                    }
                    var potentiallyModifiedKeys = current.Properties().Select(c => c.Name).Except(addedKeys).Except(unchangedKeys);
                    foreach (var k in potentiallyModifiedKeys)
                    {
                        diff[k] = FindDiff(current[k], model[k]);
                    }
                }
                break;
            case JTokenType.Array:
                {
                    var current = Current as JArray;
                    var model = Model as JArray;
                    diff["+"] = new JArray(current.Except(model));
                    diff["-"] = new JArray(model.Except(current));
                }
                break;
            default:
                diff["+"] = Current;
                diff["-"] = Model;
                break;
        }

        return diff;
    }
Dzmitry Paliakou
  • 1,587
  • 19
  • 27