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.
Asked
Active
Viewed 3.6k times
24
-
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
-
3There's a nice library available here that does exactly that: https://github.com/wbish/jsondiffpatch.net – Faheem Nov 14 '17 at 12:12
5 Answers
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
-
Doesn't the IgnoreChildOrder flag imply that the order of elements in JSON arrays is ignored? – Jørgen Fogh Aug 07 '14 at 10:15
-
Maybe, worth trying. I'm not working on a windows machine at the moment, so I can't try right now. If you try, let me know. – weston Aug 07 '14 at 10:23
-
@JørgenFogh Just call it with `CompareJson("{\"a\":[1,2]}", "{\"a\":[2,1]}");` – weston Aug 07 '14 at 11:29
-
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

Bhargava Mummadireddy
- 309
- 5
- 11
-
1How to use this function with a array of objects. I have 100 of objects in existing JArray and same on modified JArray ? – Vikash Rathee Jul 31 '16 at 03:11
-
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.

EJoshuaS - Stand with Ukraine
- 11,977
- 56
- 49
- 78

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
-
1
-
Does it take the actual structure into consideration, or is it content with finding a keys of the same name anywhere in the structure? – Maritim Mar 28 '17 at 07:59