Ally
Ally

Reputation: 4942

How do I check two JSON objects are equal?

I'm trying to discover if two JSON strings are equal.

This is what I previously tried

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?

Surely there must exist an elegant simple way to what I thought would be a common task?

Upvotes: 11

Views: 14279

Answers (3)

RL89
RL89

Reputation: 1916

Another way to compare json - Comparing 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));

Upvotes: 17

Dejan
Dejan

Reputation: 10323

You could use the Compare .NET Objects lib to check if the two object instances are equal. It knows how to compare lists, dictionaries, etc. and deep compares the whole object graph. It also supports detailed reporting of what is different and has many more features you might want to use in the future.

Upvotes: 1

Sandesh
Sandesh

Reputation: 3004

I was able to compare two JSON to some extent using the below code. For primitive classes I was able to get result to a good extent.

I hope with some more help and tweaking the below can be made more robust

    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");
        }
    }

Upvotes: 0

Related Questions