I’ve been working with JSON in .NET using Json.Net recently, going beyond simple serialization and deserialization. I want to document some useful techniques I’ve come across.
First, it’s important to differentiate between [JsonConvert](https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_JsonConvert.htm) and [JObject](https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_Linq_JObject.htm), the two primary classes for handling JSON in .NET. When there’s a .NET class matching the JSON structure, JsonConvert.DeserializeObject is used. Otherwise, JObject.Parse deserializes into a JObject, which, combined with the dynamic keyword, allows for easy manipulation of the JSON structure. One point of confusion is that JsonConvert.DeserializeObject(item), JsonConvert.DeserializeObject(item) and JsonConvert.DeserializeObject(item) return a JObject, making a JObject.Parse() redundant in these cases: [equivalent](https://stackoverflow.com/questions/23645034/jobject-parse-vs-jsonconvert-deserializeobject).
The difference between camelCase in JSON and PascalCase in C# properties can be tricky. When deserializing with JsonConvert.DeserializeObject, Json.Net attempts to match JSON camelCase properties (like “{’name’:‘Iyan’}”) with either camelCase (Person.name) or PascalCase (Person.Name) properties in our .NET classes, eliminating the need to change standard naming conventions.
| |
By default, JsonConvert serializes C# class properties using their defined names, which could lead to PascalCase JSON. To ensure camelCase JSON output, we can guide the serializer using these techniques:
| |
Now, let’s explore JObject, dynamic, and explicit/implicit conversions. Let’s illustrate with code:
| |
We know resultJObj.users is a JArray, but we need to explicitly cast it for the compiler, as it’s treated as dynamic. JArray doesn’t directly have a Select method; it’s an extension method from IEnumerable. Since extension methods and dynamic don’t work smoothly together, casting is needed so the compiler can correctly call Linq.Enumerable.Select. This Select method returns an IEnumerable, requiring another cast to dynamic for accessing the name property.
The following lines demonstrate how implicit and explicit conversions, combined with JObjects and dynamic, simplify working with JSON.
A while back, I came across [I already posted](https://deploytonenyures.blogspot.com/2015/10/deserialize-runtime-type.html) a fascinating concept: deserializing an object into a JObject and then converting a portion of it (for which we have a .NET type) using [ToObject](https://www.newtonsoft.com/json/help/html/M_Newtonsoft_Json_Linq_JToken_ToObject__1_1.htm). My previous example used a serializer with ToObject due to camelCase to PascalCase conversion. Still, as previously explained, JsonConvert handles this automatically now.
| |
The reverse can also be beneficial: taking a .NET class and serializing it to JSON with additional fields. This can be achieved with [FromObject](https://www.newtonsoft.com/json/help/html/Overload_Newtonsoft_Json_Linq_JToken_FromObject.htm) like this:
| |
The limitation here is the lack of a built-in method to serialize the JObject to camelCase JSON. A more involved approach is required, similar to the one outlined [described here](https://stackoverflow.com/questions/40244395/how-to-serialize-a-jobject-the-same-way-as-an-object-with-json-net).