How to check two JSON objects equal?

I am trying to detect if two JSON strings are equal.

This is what I tried before

var obj1 = Json.Decode("{\"ValueA\":1,\"ValueB\":2}") var obj2 = Json.Decode("{\"ValueB\":2,\"ValueA\":1}") // But then there seems to be no way to compare the two objects? 

Of course, should there be an elegant, simple way that I thought it would be a common task?

+6
source share
3 answers

Another way to compare json - Compare JSON with JToken.DeepEquals

 JObject o1 = new JObject { { "Integer", 12345 }, { "String", "A string" }, { "Items", new JArray(1, 2) } }; JObject o2 = new JObject { { "Integer", 12345 }, { "String", "A string" }, { "Items", new JArray(1, 2) } }; Console.WriteLine(JToken.DeepEquals(o1, o2)); 
+8
source

I was able to compare the two JSONs to some extent using the code below. For primitive classes, I was able to get the result pretty much.

I hope that with additional help and customization below can be made more reliable

  static void Main(string[] args) { var o = new { ValueA = "",//Comparison Works ValueB = "",//Comparison Works ValueC = new { ValueD = "", ValueE = "" },//Comparison Works ValueF = new[] { new { ValueG = "", ValueH = "" } },//Fails if the array values are out of order ValueI = new SortedDictionary<object, object>()//Comparison works }; var t = JsonConvert.DeserializeAnonymousType( "{\"ValueA\":1,\"ValueB\":2, \"ValueC\":{\"ValueE\":2,\"ValueD\":1}, \"ValueF\":[{\"ValueG\":10,\"ValueH\":25}],\"ValueI\":{\"Test1\":\"Val1\",\"Test2\":\"Val1\"}}", o); var q = JsonConvert.DeserializeAnonymousType( "{\"ValueB\":2,\"ValueA\":1, \"ValueC\":{\"ValueD\":1,\"ValueE\":2}, \"ValueF\":[{\"ValueH\":25,\"ValueG\":10}],\"ValueI\":{\"Test2\":\"Val1\",\"Test1\":\"Val1\"}}", o); var prop = t.GetType().GetProperties(); var match = true; foreach (var item in prop) { var type = item.PropertyType; if (type.IsArray) { var v1 = item.GetValue(t) as Array; var v2 = item.GetValue(q) as Array; if ((v1 != null && v2 != null)) { if ((v1.Length != v2.Length)) { match = false; break; } for (int i = 0; i < v1.Length; i++) { if (!v1.GetValue(i).Equals(v2.GetValue(i))) { match = false; break; } } } else if ((v1 == null && v2 != null) || (v1 != null && v2 == null)) { match = false; break; } } else if (type.Name.Contains("Dictionary")) { var v1 = (SortedDictionary<object, object>)item.GetValue(t); var v2 = item.GetValue(q) as SortedDictionary<object, object>; foreach (var ar in v1) { if (!v2.Contains(ar)) { match = false; break; } } } else if (!item.GetValue(t).Equals(item.GetValue(q))) { var v1 = item.GetValue(t); var v2 = item.GetValue(q); match = v1.ToString().Equals(v2.ToString()); match = false; break; } } if (!match) { Console.WriteLine("Objects do not match"); } } 
0
source

You can use Compare .NET Objects lib to check if two object instances are equal. He knows how to compare lists, dictionaries, etc. And deeply compare the entire graph of the object. It also supports a detailed message about what's different, and has many more features that you might want to use in the future.

0
source

All Articles