Nathan C. Tresch
Nathan C. Tresch

Reputation: 948

How can I detect invalid JSON containing a trailing comma with c#?

Yes, yes, I know that there are countless posts about detecting invalid Json. They all say the same thing: Try parsing or de-serializing the object. Guess what? In this case, Json.NET happily parses my malformed JSON and doens't complain. I think that I need to clarify: The only thing wrong with my JSON is a trailing comma in an array. The following illustrates my problem:

string badJson = "{ 'array' : [ {'objName1' : 'value1'}, {'objName2' : 'value2'}, {'objName3' : 'value3'}, ] }";

var obj = JObject.Parse(badJson);

The JSON isn't valid in a browser as it has a trailing comma in the array, but it's happily parsed into a jobject. Since I can't actually use serialization to detect this problem, how can I?

Note that I've tried serializing into an object, that produced the same result.

Update:

The following code will detect the comma correctly and strip it, in case anyone needs to do this:

var regex = new Regex(@"(.*?),\s*(\}|\])",(RegexOptions.Multiline));

var cleanJson = regex.Replace(content, m => String.Format("{0} {1}",m.Groups[1].Value,m.Groups[2].Value));

Another possible solution would be to use the .NET Json parser with my object model. Another solution that worked for me was to parse the object into a Jobject then serialize it again. That allowed me to use the Json where I needed to. This was much slower than the above regex, so I ultimately used the regex.

Upvotes: 5

Views: 3663

Answers (2)

Peter Gluck
Peter Gluck

Reputation: 8236

You could strip out the offending comma with a regular expression.

Regex.Replace(badJson, "}\s*,+\s*]", "} ]");

This pattern will find a comma between a closing brace and a closing bracket with an arbitrary (0 or more) quantity of whitespace characters surrounding the comma.

Upvotes: 0

Robert Levy
Robert Levy

Reputation: 29073

if Regex.IsMatch(badJson, "^.*,\s*]\s*}$") 
   throw new Exception("hey that's bad json");

Upvotes: 2

Related Questions