Handling Non-Standard JSON in .NET Using System.Text.Json
Published: 2024-01-04 08:24:36

JSON (JavaScript Object Notation) is a widely used data format for exchanging information between applications and systems. While JSON has a well-defined specification, real-world JSON data sometimes includes non-standard additions such as comments, trailing commas, or encoded numbers.

The .NET library System.Text.Json offers efficient JSON serialization and deserialization. By default, it strictly adheres to the JSON specification. However, System.Text.Json also provides options to process non-standard JSON content.

In this article, we will explore how to configure System.Text.Json to handle JSON data that includes:

- Comments
- Trailing commas
- Number Handling

##Allowing Comments in JSON##

Comments are not permitted in valid JSON. However, we may encounter JSON strings with comments for documentation purposes:

```
JSON:
{
  "name": "PrameshKc", // name of the user
  "age": 30
}
```

If we try to deserialize the above JSON, we will get the following exception:
`System.Text.Json.JsonException: '/' is an invalid start of a property name. Expected a '"'. Path: $ | LineNumber: 0`

To allow comments when deserializing, we need to set the `JsonSerializerOptions.ReadCommentHandling` property:


```
var options = new JsonSerializerOptions
{
  ReadCommentHandling = JsonCommentHandling.Skip
};


var data = JsonSerializer.Deserialize<User>(json, options);
```

This will skip over any comments in the JSON string during deserialization.

##Ignoring Trailing Commas##

Trailing commas are also invalid in JSON:

```
JSON:
{
  "name": "PrameshKc",
  "age": 30,
}
```
If we try to deserialize the above JSON, we will get the following exception:
`System.Text.Json.JsonException: The JSON object contains a trailing comma at the end which is not supported in this mode.
`
To permit trailing commas, set JsonSerializerOptions.AllowTrailingCommas to true:


```
var options = new JsonSerializerOptions
{
  AllowTrailingCommas = true  
};

var data = JsonSerializer.Deserialize<Data>(json, options);
```
Now any trailing commas will be ignored when deserializing.

##Customizing Number Handling##

Sometimes JSON may encode numbers as strings:

```
json
{
  "temp": "37" 
}
```
If we try to deserialize the above JSON, we will get the following exception:

`System.Text.Json.JsonException: 'The JSON value could not be converted to System.Int32. Path: $.DegreesCelsius | LineNumber: 2 | BytePositionInLine: 21.'`

We can customize how numbers are handled using the JsonNumberHandling option:


```
var options = new JsonSerializerOptions
{
  NumberHandling = JsonNumberHandling.AllowReadingFromString
};

var data = JsonSerializer.Deserialize<Weather>(json, options);
```

This will permit deserializing string-encoded numbers to numeric properties.

###Conclusion###
The System.Text.Json API provides many options to handle non-standard JSON formatting like comments, trailing commas, and unquoted names. By configuring JsonSerializerOptions, we can customize how .NET handles invalid JSON data during serialization and deserialization.
Happy coding! 😊

[@prameshkc ](https://www.linkedin.com/in/prameshkc/)