JSON.NET – Validate the String which is JSON Array / JSON Object

JSON.NET – Validate the String which is JSON Array / JSON Object

 
   using Newtonsoft.Json;
   using Newtonsoft.Json.Linq;
 
   private static bool IsJSONArray(string strInput)
   {
     strInput = strInput.Trim();
 
     if (strInput.StartsWith("[") && strInput.EndsWith("]"))
     {
       try
       {
         var obj = JArray.Parse(strInput);
         return true;
       }
       catch (JsonReaderException jex)
       {
         return false;
       }
       catch (Exception ex)
       {
         return false;
       }
     }
     else
     {
       return false;
     }
   }
 
   private static bool IsJSONObject(string strInput)
   {
     strInput = strInput.Trim();
 
     if (strInput.StartsWith("{") && strInput.EndsWith("}"))
     {
       try
       {
         var obj = JObject.Parse(strInput);
         return true;
       }
       catch (JsonReaderException jex)
       {
         return false;
       }
       catch (Exception ex)
       {
         return false;
       }
     }
     else
     {
       return false;
     }
   }
 

It can be used to validate the Object Field Value which is from SQL Server – JSON nvarchar Field.