1

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

Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249
Dominik Lemberger
  • 2,287
  • 2
  • 21
  • 33
  • Take a look at this http://stackoverflow.com/questions/2583472/regex-to-validate-json. – oopbase Mar 23 '17 at 08:07
  • Possible duplicate of [Regex to validate JSON](http://stackoverflow.com/questions/2583472/regex-to-validate-json) – Maksim Simkin Mar 23 '17 at 08:09
  • it's probably not the best way to do it, but you could, deserialize, then serialize again and compare both strings, if they are not identical it's a good indication of problems with the JSON. I would not recommend this if you are dealing with large JSON files. – J. Tuc Mar 23 '17 at 08:13
  • 1
    Also this regex doesnt validate right - Because it validates the 2 Head Token on same Level as right, but it should be false as there cant be same Token on the same Level. @J. Tuc i guess i will go with this, if there really is no working validation - but yeah its not really nice – Dominik Lemberger Mar 23 '17 at 08:15

1 Answers1

-1

You can modify this code to check elements from JToken.Parse() result. If values are duplicated you can return false

  • The JToken.Parse result is never duplicated because it uses some kind of dictionary - thats exactly my problem. It doesnt raise and error, it just deletes duplicated nodes and creates a new - non duplicated - object instead of raising an error that there are duplicates – Dominik Lemberger Mar 23 '17 at 08:48