Deserializing JSON that contains an embedded JSON string

When you send mail, you put a letter in an envelope and put routing information on the envelope. The postal service uses the routing information to deliver the envelope to a specific mailbox. The mailbox owner then opens the envelope and reads the letter. Sometimes you may need to deal with the JSON equivalent of … Read more

System.Text.Json.JsonException: A possible object cycle was detected which is not supported

When you use System.Text.Json.JsonSerializer to serialize an object that has a cycle, you’ll get the following exception: System.Text.Json.JsonException: A possible object cycle was detected which is not supported. This can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 0. This is the same problem … Read more

C# – Using SqlDataReader to process multiple result sets

In this article I’ll show how to use the SqlDataReader ADO.NET class in two scenarios involving multiple result sets: Batches – When you execute multiple SELECTs in a single query. Each SELECT returns a different result set. You use a single reader to process the batch. Nested Queries – When you use multiple SqlDataReaders on … Read more

Modifying JSON in SQL Server

There are two ways to modify JSON data in a SQL Server table: Use the JSON_MODIFY() function in an UPDATE statement to change individual properties inside the JSON data. Completely replace the JSON data with a regular UPDATE statement. In this article, I’ll show how to insert, update, and delete JSON data by using the … Read more

Querying JSON in SQL Server

In SQL Server 2016 they added support for handling JSON data. You can add JSON data, query it, and modify it. The JSON API in SQL Server is well-suited for simple scenarios where you only need to do basic querying and manipulation. For advanced querying, the SQL Server JSON API gets complicated and hard to … Read more

Python – Make an HTTP request and use the returned JSON

In Python the requests module makes it simple to make HTTP requests and handle the returned JSON. I have a web API that returns exchange rates for a given currency. In this article, I’ll show how to use the requests module to call this endpoint and use the returned JSON. This will include error handling, … Read more

C# – How to read custom configurations from appsettings.json

The appsettings.json file is a convenient way to store and retrieve your application’s configuration. You can add it to any project and then use the Microsoft.Extensions.Configuration library to work with it. Since appsettings.json is just a JSON file, you can add anything to it (as long as it’s valid JSON). Compared with working with the … Read more

C# – Case sensitivity in JSON deserialization

By default Newtonsoft does case insensitive JSON deserialization and System.Text.Json does case sensitive JSON deserialization. Case sensitivity comes into play when a JSON string is being deserialized into an object. If you’re using case sensitive deserialization, then keys in the JSON string must match type names exactly, otherwise it won’t deserialize the class/property with the … Read more

System.Text.Json can’t serialize Dictionary unless it has a string key

The built-in JSON serializer in .NET Core can’t handle serializing a dictionary unless it has a string key. When I run this code I get the following exception: System.NotSupportedException: The collection type ‘System.Collections.Generic.Dictionary`2[System.Int32,System.String]’ is not supported. It can only serialize dictionaries with string keys. This is bizarre, and another reason to stick with Newtonsoft for … Read more

C# – Deserialize JSON to dynamic object

If you want to deserialize JSON without having to create a bunch of classes, use Newtonsoft.Json like this: Now you can use this object like any other object. Example Add the Newtonsoft.Json reference Code Output: JSON Dynamic vs ExpandoObject If you specify <dynamic> instead of <ExpandoObject> it’ll create a JObject. In older versions of Newtonsoft, … Read more