How to use JsonDocument to read JSON in C#
By Tan Lee Published on Mar 06, 2025 154
Consider the following JSON object representing a book:
{ "title": "The Great Gatsby", "author": "F. Scott Fitzgerald", "year": 1925, "genres": ["Fiction", "Classic", "Literature"] }
If you're only interested in the book's title, you can use JsonDocument
to read the JSON string and extract the title value without deserializing the entire structure:
using System.Text.Json; string json = /* your JSON string here */; using (var jsonDoc = JsonDocument.Parse(json)) { JsonElement title = jsonDoc.RootElement.GetProperty("title"); Console.WriteLine($"Book title = {title.GetString()}"); }
Output:
Book title = The Great Gatsby
How to use JsonDocument
in C#?
Check if a Property Exists
When using JsonElement.GetProperty()
, a KeyNotFoundException
is thrown if the property doesn't exist. To check for a property without throwing an exception, use TryGetProperty()
:
using (var jsonDoc = JsonDocument.Parse(json)) { if (jsonDoc.RootElement.TryGetProperty("year", out JsonElement year)) { Console.WriteLine($"Year property exists and has int value = {year.GetInt32()}"); } }
Output:
Year property exists and has int value = 1925
Loop Through a JSON Object’s Properties
To loop through a JSON object's properties, use EnumerateObject()
.
For example, let's loop through the properties of the book JSON:
using (var jsonDoc = JsonDocument.Parse(json)) { foreach (var property in jsonDoc.RootElement.EnumerateObject()) { Console.WriteLine($"{property.Name} ValueKind={property.Value.ValueKind} Value={property.Value}"); } }
Output:
title ValueKind=String Value=The Great Gatsby author ValueKind=String Value=F. Scott Fitzgerald year ValueKind=Number Value=1925 genres ValueKind=Array Value=[]
Loop Through a JSON Array
To loop through the values in a JSON array, use EnumerateArray()
.
For example, if you want to loop through and print the genres of the book:
using (var jsonDoc = JsonDocument.Parse(json)) { JsonElement genres = jsonDoc.RootElement.GetProperty("genres"); foreach (var genre in genres.EnumerateArray()) { Console.WriteLine($"Genre: {genre.GetString()}"); } }
Output:
Genre: Fiction Genre: Classic Genre: Literature
Deserialize a Nested Object
You can also extract a nested object using GetProperty()
and then deserialize it into a specific type.
Consider a more complex JSON structure where you want to extract and deserialize information about the author:
{ "title": "The Great Gatsby", "author": { "name": "F. Scott Fitzgerald", "birthYear": 1896 }, "year": 1925 }
Using JsonDocument
, you can extract the author details like this:
using (var jsonDoc = JsonDocument.Parse(json)) { var authorElement = jsonDoc.RootElement.GetProperty("author"); var author = authorElement.Deserialize<Author>(jsonOptions); Console.WriteLine($"Author: {author.Name}, Birth Year: {author.BirthYear}"); }
This would return an Author
object with the appropriate values.
This revised example illustrates how to utilize JsonDocument
to read JSON data effectively in C#, using a book-related JSON structure.
- Serialize and Deserialize a Multidimensional Array in JSON using C#
- JSON object contains a trailing comma at the end which is not supported
- How to use JsonExtensionData in C#
- Serialize a tuple to JSON in C#
- Deserialize JSON using different property names in C#
- Deserialize JSON to a derived type in C#
- Deserialize JSON to a dictionary in C#
- Deserialize a JSON array to a list in C#