0

I have a Windows Service that frequently monitors the .json file structure, it has always worked correctly until yesterday where this .json could not be read correctly by our applications but was considered as valid!:

{
    "infohead": {
        "indicator": "12",
        "istesting": "ABC_12_219",
        "isdate": "2021-08-11 10:02:12"
    },
    "headers": {
        "url": "https://www.blahblah.com/"
    },
    "advertisements": [,
        {
            "type": "arc",
            "adType": "1",
            "appn": {
                "x": 1241,
                "y": 1241,
                "z": 1243,              
            }
        },
        {
            "type": "arc2",
            "adType": "1",
            "appn": {
                "x": 1441,
                "y": 1441,
                "z": 1443,                          
            }
        }
    ]   
}

if it loads in a browser I get the error:

SyntaxError: JSON.parse: unexpected character at line 10 column 21 of the JSON data

C # somehow detects that this is correct, the array "advertisements" has three objects, one with a empty value and the other with a value.

"advertisements": [,
        {
            "type": "arc",
            "adType": "1",
            "appn": {
                "x": 1241,
                "y": 1241,
                "z": 1243,              
            }
        },
        {
            "type": "arc2",
            "adType": "1",
            "appn": {
                "x": 1441,
                "y": 1441,
                "z": 1443,                          
            }
        }
    ]   

I have also used the methods described in this question,

How to make sure that string is valid JSON using JSON.NET

I didn´t want to validate using a JSON schema (Validate Json schema C#), there is another solution to perform the validation?

Jorgesys
  • 124,308
  • 23
  • 334
  • 268
  • 1
    see here: https://stackoverflow.com/questions/44543835/invalidate-trailing-commas-in-json-using-net – Donal Aug 11 '21 at 21:00
  • 1
    Json.NET is known to be forgiving of certain types of malformed JSON. See [Support "strict mode" for RFC7159 parsing #646](https://github.com/JamesNK/Newtonsoft.Json/issues/646#issuecomment-356194475): *Do you happen to have the list that identifies all the ways Json.Net is more lenient?* *Off the top of my head:... Empty commas, e.g. `[,,,]`* – dbc Aug 11 '21 at 21:11
  • 1
    System.Text.Json is completely unforgiving by default. If you just want to check that JSON is well-formed try loading into a `JsonDocument` or reading through it with `Utf8JsonReader` using `TrySkip()` as shown [here](https://stackoverflow.com/a/59983507/3744182). – dbc Aug 11 '21 at 21:17

1 Answers1

0

I tried with the suggested solutions and even I tried using a REGEX with no success, in fact I found something interesting, for example, using this method if I try to deserialize the content of my "json":

WebClient client = new WebClient();
string jsonContent= client.DownloadString(jsonFile);
dynamic myJson = Newtonsoft.Json.JsonConvert.DeserializeObject(jsonContent);

I got no exception and the content shows an "undefined" value as an object inside the JsonArray:

"advertisements": [undefined,
        {
            "type": "arc",
            "adType": "1",
            "appn": {
                "x": 1241,
                "y": 1241,
                "z": 1243,              
            }
        },
        {
            "type": "arc2",
            "adType": "1",
            "appn": {
                "x": 1441,
                "y": 1441,
                "z": 1443,                          
            }
        }
    ]   

So for now my "solution" was using JToken.Parse(...) and find for "undefined" string inside the content :-( .

   WebClient client = new WebClient();
   string myJSON = client.DownloadString(jsonFile);
   var obj = JToken.Parse(myJSON);
   if (obj.ToString().Contains("undefined")) //Check for "undefined" values.
    {
      //Json content not valid for processing.                 
    }
    else
    {
     //Json content is valid!
    }
Jorgesys
  • 124,308
  • 23
  • 334
  • 268