How to Work with JSON in .Net

Json.NET by Newtonsoft is the de-facto standard when it comes to work with JSON in .Net. The library is very popular, offers great flexibility and is easy to use. Let’s look on what you have to do to serialize and deserialize objects to JSON.

 

Installation

Open the Package Management window in Visual Studio and search for the Newtonsoft.Json NuGet package to add it to your project:

Search for Newtonsoft.Json

Or install it with this command in the Package Manager Console:

All your questions should be answered in the well-written official documentation, that includes more than 100 examples on how to use Json.NET.

 

Serialize and deserialize objects to and from JSON

For my examples I use these the class BlogPost and an enum called State to show how you can serialize those often-used combinations:

All you need to serialize your objects to JSON is to call JsonConvert.SerializeObject:

The Formatting.Indented flag is optional. It will intent your JSON and makes it more readable:

Turning JSON back into an object is even simpler. All it takes is a call to JsonConvert.DeserializeObject, a type you want to serialize it back to and your JSON string:

Your JSON is deserialized into an object

 

Serialize and deserialize lists of objects to and from JSON

Serializing a list of objects is no different than using a single object:

This results in a JSON array with three posts:

Turning that JSON back to a list of objects uses a slightly different command. This time you want a list of BlogPosts what you need to say explicitly in the type:

Your JSON is deserialized into a list of objects

If you do not specify the list and instead use BlogPost, you will get this error:

Cannot deserialize the current JSON array (e.g. [1,2,3]) into type ‘JsonExamples.BlogPost’ because the type requires a JSON object (e.g. {“name”:”value”}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {“name”:”value”}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.

If you read the error message you see another great point in Json.NET – it helps you to do use its API the right way with useful error messages.

 

Influence the serialization

The default serialization includes NULL values and writes the property names exactly as you wrote them in your class:

Especially when you exchange JSON with other systems, then you may need a different serialization approach. You can change all the behaviour using a JsonSerializerSettings object that you can pass into the SerializeObject method. To use camelCase for property names, the MicrosoftDateFormat and ignores Null values you can use these settings:

 

Influence the serialization even more

Sometimes you need to have even more control on how properties are serialized to JSON. In this case you can use JsonPropertyAttribute to use a different name or JsonConverter to use a different strategy to serialize Enum values. You put this kind of attributes on your business class:

If you now serialize your object you don’t need to add a settings object. The object itself has all information on its serialization inside and Json.NET will use it accordingly:

 

Ignoring properties

If you have some properties or fields you do not want to serialize, you can use the annotation [JsonIgnore] to supress them:

Add this annotation to the Text property of the Comment class from above and the field is no longer present in the JSON file:

 

Conclusion

Working with JSON in .Net is a super simple task thanks to Json.NET from Newtonsoft. All you need to know is how to serialize and deserialize objects and lists and you can solve all tasks involving JSON. Should something special occur you most like find the specific command in their great documentation.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.