C# – Serialize a dictionary to JSON

When you want to convert a dictionary to a JSON string, you can use the built-in JsonSerializer (from System.Text.Json) to serialize the dictionary. Here’s an example: Note: This is passing in WriteIndented=true to pretty print the JSON string for readability. This serializes the dictionary to a JSON string with the following format: I’ll show more … Read more

C# – Populate an existing object with JSON

Normally when you’re working with JSON, you deserialize it to a target type and get back an initialized and fully populated object. How about if you need to initialize an object yourself, and then populate it with JSON later? For example, let’s say you want to load the following JSON array into a case-insensitive HashSet: … Read more

C# – Deserialize a JSON array to a list

When you’re working with a JSON array, you can deserialize it to a list like this: This deserializes all of the objects in the JSON array into a List<Movie>. You can use this list object like usual. Note: All examples will use System.Collections.Generic and System.Text.Json. I’ll exclude the using statements for brevity. Example – JSON … Read more

C# – Deserialize JSON as a stream

Here’s an example of deserializing JSON from a file as a stream with System.Text.Json: Stream deserialization has three main benefits: In this article, I’ll go into details about these benefits and show a few other stream serialization scenarios. Benefits of deserializing as a stream Performance There are two ways to deserialize JSON: Deserializing a stream … Read more

C# – Get inserted identity value with Dapper

When you insert a record into a table with an identity column, the value for the identity column is automatically generated for you. The simplest way to get the inserted identity value is to put OUTPUT INSERTED.<identity column name> in the insert statement: To get the output value with Dapper, use ExecuteScalar<int>(): This inserts the … Read more

C# – Create a custom JsonConverter for System.Text.Json

Most of the time System.Text.Json will get you want you want. You can pass in options to control JSON serialization and deserialization to a certain extent. But sometimes you’ll run into scenarios where you need to customize how it handles a specific type. This is where JsonConverter comes in. You can customize serialization / deserialization … Read more

Modifying JSON in SQL Server

There are two ways to modify JSON data in a SQL Server table: In this article, I’ll show how to insert, update, and delete JSON data by using the JSON_MODIFY() function. I’ll also show how to deal with JSON arrays. Note: This is part 2 of the mini-series about the SQL Server JSON API. The … Read more

C# – Deserialize JSON to dynamic object

If you want to deserialize JSON without having to create a bunch of classes, you can either deserialize to a dictionary or deserialize to a dynamic object with Newtonsoft.Json. Here’s an example. Let’s say you want to deserialize the following JSON: To deserialize this to a dynamic object with Newtonsoft, use JsonConvert.DeserializeObject<dynamic>: This outputs the … Read more