Compare two JSON Objects

Hi Guys

Recently I encountered a situation in which – the create application works in the front end but it ain’t working when calling thr’ API. So the real problem was – one API variable was renamed by a developer but didn’t updated in the API Documentation page used by the other companies.

The Solution is to compare the Actual Server Response with the Expected Server Response.

I had wrote a small code to compare two Json String

public static void CompareObjectKeys(JObject srcO, JObject tgtO) // source - new, target - old, existing for comparison
       {
           var returnString = new StringBuilder();
           foreach (var sourcePair in srcO)
               switch (sourcePair.Value.Type) {
                   case JTokenType.Object:
                       if (tgtO.SelectToken(sourcePair.Key) == null) // if the object not found in the target
                           ReportHelpers.Log("Key " + sourcePair.Key + " not found" + Environment.NewLine,null, Status.Fail);
                       else if (tgtO.GetValue(sourcePair.Key).Type != JTokenType.Object)
                           ReportHelpers.Log("Key " + sourcePair.Key + " is not an object in target" + Environment.NewLine, null, Status.Fail);
                       else
                           CompareObjectKeys(sourcePair.Value.ToObject<JObject>(), tgtO.GetValue(sourcePair.Key).ToObject<JObject>());
                       break;
                   case JTokenType.Array:
                       if (tgtO.SelectToken(sourcePair.Key) == null)
                           ReportHelpers.Log("Key " + sourcePair.Key + " not found" + Environment.NewLine, null, Status.Fail);
                       else
                           CompareArrays(sourcePair.Value.ToObject<JArray>(), tgtO.GetValue(sourcePair.Key).ToObject<JArray>(), sourcePair.Key);
                       break;
                   default:
                       var expected = sourcePair.Value;
                       var actual = tgtO.SelectToken(sourcePair.Key);
                       if (actual == null) {
                           ReportHelpers.Log("Key " + sourcePair.Key + " not found" + Environment.NewLine,null,Status.Fail);
                       }
                       else {
                           if (!JToken.DeepEquals(expected, actual))
                               ReportQuick.Log("Key " + sourcePair.Key + ": " + sourcePair.Value + " !=  " + tgtO.Property(sourcePair.Key).Value + Environment.NewLine);
                       }
                       break;
               }
           
       }

 

private static StringBuilder CompareArrays(JArray sAr, JArray tAr, string arrayName = "") {
           var returnString = new StringBuilder();
           for (var index = 0; index < sAr.Count; index++) {
               var expected = sAr[index];
               if (expected.Type == JTokenType.Object) {
                   var actual = index >= tAr.Count ? new JObject() : tAr[index];
                   CompareObjectKeys(expected.ToObject<JObject>(), actual.ToObject<JObject>());
               }
               else {
                   var actual = index >= tAr.Count ? "" : tAr[index];
                   if (!JToken.DeepEquals(expected, actual))
                       if (string.IsNullOrEmpty(arrayName))
                           ReportHelpers.Log("Index " + index + ": " + expected
                                             + " != " + actual + Environment.NewLine);
                       else
                           ReportQuick.Log("Key " + arrayName + "[" + index + "]: " + expected + " != " + actual + Environment.NewLine);
               }
           }
           return returnString;
       }

 

Leave a Reply

Your email address will not be published. Required fields are marked *