JSON Serialization
Convert strongly typed objects to and from JSON.
Introduction
System.Text.Json is the built-in high-performance JSON API for modern .NET.
Serialize converts a record into JSON. Deserialize recreates the typed value and nullable handling covers malformed or empty input.
Runnable C# example
Program.cs
using System;
using System.Text.Json;
record Product(string Name, decimal Price);
class Program
{
static void Main()
{
var json = JsonSerializer.Serialize(new Product("Book", 12.5m));
var product = JsonSerializer.Deserialize<Product>(json);
Console.WriteLine($"{product?.Name}: {product?.Price}");
}
}
Book: 12.5Practice with the debugger
Set a breakpoint on an important assignment or condition, click Debug, and inspect the local values before stepping to the next line.
Practice with a challenge
Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.
- Sum a Flat JSON Object Parse a flat JSON object whose values are integers and add up the values. Easy
- Build a JSON Object Turn keys and values into a JSON object string, escaping quotes and backslashes correctly. Easy