I tried using code I found in this answer :-
strInput = strInput.Trim();
if ((strInput.StartsWith("{") && strInput.EndsWith("}")) || //For object
(strInput.StartsWith("[") && strInput.EndsWith("]"))) //For array
{
try
{
var obj = JToken.Parse(strInput);
return true;
}
catch (JsonReaderException jex)
{
//Exception in parsing json
Console.WriteLine(jex.Message);
return false;
}
catch (Exception ex) //some other exception
{
Console.WriteLine(ex.ToString());
return false;
}
}
else
{
return false;
}
Problem with this code is, that it validates wrong JSON Files as right, because JToken.Parse automatically deletes Elements that are doubled in the sequence for example:
{
"Body" : {
"Data" : {}
},
"Head" : {
"RequestArguments" : {
"Scope" : ""
},
"Status" : {
"Code" : 255,
"Reason" : "CGI-Args: Invalid parameter '' for Scope.",
"UserMessage" : ""
},
"Timestamp" : "2017-01-24T13:15:33+01:00"
},
"Head" : {
"RequestArguments" : {
"Scope" : ""
},
"Status" : {
"Code" : 255,
"Reason" : "CGI-Args: Invalid parameter '' for Scope.",
"UserMessage" : ""
},
"Timestamp" : "2017-01-24T13:15:33+01:00"
}
}
Here there are 2 Head Token on the same Level which are not allowed, but JToken automatically parses away one of the 2 Head Tokens so the resulting Object is valid