Deserialize a JSON array to a list in C#
By FoxLearn 2/5/2025 8:17:09 AM 172
First, define a C# class that matches the structure of the JSON objects in the array.
For example, if the JSON contains information about books, you can create a Book
class:
public class Book { public string Title { get; set; } public string Author { get; set; } public int Year { get; set; } public double Price { get; set; } }
When working with a JSON array, you can deserialize it into a list in a straightforward manner:
using System.Collections.Generic; using System.Text.Json; var bookList = JsonSerializer.Deserialize<List<Book>>(json);
This deserializes all the objects in the JSON array into a List<Book>
, which you can then work with as usual.
Next, you will have a JSON array that you want to deserialize.
Valid JSON can be an object (i.e., {}
) or an array (i.e., []
).
Below is an example of a JSON array that contains 5 book objects:
[ {"Title": "1984", "Author": "George Orwell", "Year": 1949, "Price": 9.99}, {"Title": "To Kill a Mockingbird", "Author": "Harper Lee", "Year": 1960, "Price": 14.99}, {"Title": "The Great Gatsby", "Author": "F. Scott Fitzgerald", "Year": 1925, "Price": 10.99}, {"Title": "Moby Dick", "Author": "Herman Melville", "Year": 1851, "Price": 12.50}, {"Title": "Pride and Prejudice", "Author": "Jane Austen", "Year": 1813, "Price": 8.99} ]
Now, you can use the JsonSerializer.Deserialize<T>()
method to deserialize the JSON array into a List<Book>
.
using System.Text.Json; var list = JsonSerializer.Deserialize<List<Book>>(booksJson); Console.WriteLine($"There are {list.Count} books.");
Output:
There are 5 books.
Deserialize and yield one object at a time
If you don’t want to load all the objects into memory at once, you can deserialize and yield one object at a time using DeserializeAsyncEnumerable()
.
using var bookJsonStream = new MemoryStream(Encoding.UTF8.GetBytes(booksJson)); await foreach (var book in JsonSerializer.DeserializeAsyncEnumerable<Book>(bookJsonStream)) { ProcessBook(book); }
In this example, we are converting a string into a MemoryStream
. It’s recommended to work with streams directly, such as reading from a file or an HTTP response stream.
Non-array collection of objects
Sometimes you will need to handle collections of objects stored in a JSON object instead of an array. Here’s an example:
{ "1984": { "Author": "George Orwell", "Year": 1949, "Price": 9.99 }, "To Kill a Mockingbird": { "Author": "Harper Lee", "Year": 1960, "Price": 14.99 }, "The Great Gatsby": { "Author": "F. Scott Fitzgerald", "Year": 1925, "Price": 10.99 } }
This is a collection of key/value pairs (where the key is the book title and the value is the book object). You can deserialize it into a Dictionary<string, Book>
:
var bookMap = JsonSerializer.Deserialize<Dictionary<string, Book>>(booksJson);
Keep things simple by deserializing into a class that matches the structure of the JSON.
If you're using Newtonsoft.Json instead of System.Text.Json
, here's how you can deserialize the JSON array into a list:
using Newtonsoft.Json; var books = JsonConvert.DeserializeObject<List<Book>>(booksJson); Console.WriteLine($"Average price = ${books.Average(b => b.Price)}");
Output:
Average price = $11.09
- Deserialize JSON using different property names in C#
- Deserialize JSON to a derived type in C#
- Deserialize JSON to a dictionary in C#
- Serialize a tuple to JSON in C#
- Serialize and Deserialize a Multidimensional Array in JSON using C#
- Modifying Date Format for JSON Serialization in C#
- Serialize anonymous types with System.Text.Json in C#
- Serialize to JSON in Alphabetical Order in C#