| Muhammad Waseem

About Newsletter
Books
30 .NET Tips
Sponsorship
How to use System.Text.Json in .NET
Apr 21, 2023
3 min read

How to use System.Text.Json in .NET

Sponsor this Newsletter

Why We Need JSON Handlers

We have multiple formats to represent data but JSON has got the big fan following , it is extensively used for sending-receiving data in applications. It is easy to read and flexible , but to work our application properly we need something that can convert the data into forms which are easy to understand by us and compatible with our data models. That’s where JSON handlers come.

Famous Names in JSON Handling World

We have two libraries that have downloads in billions at Nuget Package Manager :

  • Newtonsoft.Json (3.07B Downloads)
  • System.Text.Json (1.01B Downloads)

I have covered first one in my LinkedIn Post , today we are going to explore System.Text.Json which is owned by Microsoft.

How to use System.Text.Json

Install Nuget Package System.Text.Json and use it BINGO! Let’s see simple demonstration.

using System.Text.Json;

var dotnetNewsletter = new Newsletter
{
    Name = "Waseem Newsletter",
    Category = ".NET",
    IssuesPerMonth = 4
};

string json = JsonSerializer.Serialize(dotnetNewsletter);

var newsletter = JsonSerializer.Deserialize<Newsletter>(json);

public class Newsletter
{
    public string Name { get; set; } = string.Empty;
    public string Category { get; set; } = string.Empty;
    public int IssuesPerMonth { get; set; }
}

It has got some nice attributes as well that can be helpful , let’s see few of them

JsonRequired Attribute
using System.Text.Json;
using System.Text.Json.Serialization;

string json = "{\"Name\":\"Waseem Newsletter\",\"Category\":\".NET\"}";
// Throws JsonException because requested prop is missing
var newsletter = JsonSerializer.Deserialize<Newsletter>(json);

public class Newsletter
{
    public string Name { get; set; } = string.Empty;
    public string Category { get; set; } = string.Empty;

    [JsonRequired]
    public int IssuesPerMonth { get; set; }
}
JsonPropertyName Attribute
using System.Text.Json;
using System.Text.Json.Serialization;

var newsletter = new Newsletter
{
    Name = "C# Corner",
    Category = "Programming",
    IssuesPerMonth = 12
};

string json = JsonSerializer.Serialize(newsletter);
// {"NewsletterName":"C# Corner", "Category": "Programming", "IssuesPerMonth":12}
Console.WriteLine(json);

class Newsletter
{
    [JsonPropertyName("NewsletterName")]
    public string Name { get; set; } = string.Empty;

    public string Category { get; set; } = string.Empty;

    public int IssuesPerMonth { get; set; }
}

On similar fashion it has got few more attributes as well that you can explore

  • JsonIgnore
  • JsonConverter

quote

This article was originally published at https://mwaseemzakir.substack.com/ on Apr 21, 2023 .

Whenever you're ready, there are 3 ways I can help you:

  1. Subscribe to my youtube channel : For in-depth tutorials, coding tips, and industry insights.
  2. Promote yourself to 9,000+ subscribers : By sponsoring this newsletter
  3. Patreon community : Get access to all of my blogs and articles at one place
Previous Next

Subscribe to Newsletter

Join 9,000 Software Engineers

Buy Me a Coffee

Enjoy my articles? Support me by buying a coffee!

Buy Me a Coffee

Muhammad Waseem

Resources
  • Books
  • Courses
Newsletter
  • Articles
  • Sponsorship
Books
  • 30 .NET Tips
  • 100 .NET Tips (Soon)
Author
  • About Us
  • Contact Us
Policy
  • Privacy Policy
  • Terms and Conditions
Interview
  • C# & .NET
  • Web API

Join my .NET newsletter and stay updated!

© 2025 Muhammad Waseem. All rights reserved.