
This article was written in 2023. Versions, tools and recommendations may have changed since then. Newer posts are collected under .NET.
System.Text.Json is currently the most modern way to handle Json files efficiently. Efficiency here also means that memory sparing files are read in.
A very efficient way to handle Json files is not to load them into a string first, but to implement the deserialization via streams. This keeps the application more scalable and performant.
System.Text.Json has this built in, so such an implementation can be done within a few lines.
1// Create Client
2BlobContainerClient container = new(_options.ConnectionString, _options.ContainerName);
3BlobClient blobClient = container.GetBlobClient(_options.BlobPath);
4
5// check if blob exists
6Azure.Response<bool> exists = await blobClient.ExistsAsync(ct).ConfigureAwait(false);
7if (exists.Value is false)
8{
9 return null;
10}
11
12// Get Blob Stream
13BlobOpenReadOptions readOptions = new(allowModifications: false);
14using Stream stream = await blobClient.OpenReadAsync(readOptions, ct).ConfigureAwait(false);
15
16// read json
17JsonSerializerOptions jsonOpions = new();
18
19MyDeserializationTargetClass? myDeserializedObject = await JsonSerializer
20 .DeserializeAsync<MyDeserializationTargetClass>(stream, jsonOpions, ct)
21 .ConfigureAwait(false);Related articles

- 17 min read
Automatic TTL in Azure Cosmos DB with .NET
Temporary data is easy to create and surprisingly difficult to remove reliably. Sessions, idempotency keys, import buffers, transient …

- 11 min read
Top 10 recommendations for .NET developers working with Azure Cosmos DB
Azure Cosmos DB has a habit of exposing architectural weaknesses very early. Classic data-access patterns that work perfectly fine with …

- 14 min read
Local Aspire Development with Azure Cosmos DB and the Preview Emulator
Distributed applications tend to feel straightforward until a real cloud dependency enters the picture. Azure Cosmos DB is a good example. …
Let's Work Together
Looking for an experienced Platform Architect or Engineer for your next project? Whether it's cloud migration, platform modernization or building new solutions from scratch - I'm here to help you succeed.
