Comprehensive Guide to Using JSON and Newtonsoft.Json in .NET

İbrahim Emre POLAT
6 min readJun 14, 2024

Introduction to JSON

JSON (JavaScript Object Notation) is a lightweight data-interchange format that is easy for humans to read and write, and easy for machines to parse and generate. JSON is widely used for data exchange between a server and a web application, as well as in many other scenarios where structured data needs to be transmitted or stored.

Why We Use JSON

  • Human-Readable: JSON is written in a text format that is readable and easy to understand, which makes debugging and development straightforward.
  • Language Independent: JSON is language-agnostic, meaning it can be used across different programming languages, making it ideal for cross-platform data interchange.
  • Lightweight: JSON is compact and lightweight, which is efficient for data transmission over networks.
  • Interoperability: JSON’s format is based on key-value pairs, making it compatible with many modern programming languages and libraries.

Common Use Cases for JSON

JSON is used in various domains, each benefiting from its simplicity and versatility. Below are some common use cases with examples.

Microservices

In a microservices architecture, services often communicate with each other using message brokers like Kafka, RabbitMQ, and Azure Service Bus. These services need a standardized format to serialize and deserialize data, and JSON is commonly chosen for this purpose due to its aforementioned benefits.

For instance, when a microservice sends a message containing user information to another service, it often uses JSON to format this data. Here is an example of a JSON message:

{
"userId": 12345,
"userName": "john_doe",
"email": "john.doe@example.com"
}

This JSON message can be published to a message queue or topic and consumed by another service, which will then deserialize it into a corresponding object.

Web APIs

RESTful APIs commonly use JSON to format data sent to and received from clients. For example, a weather API might return weather data in JSON format:

{
"location": "New York",
"temperature": "20°C",
"conditions": "Sunny"
}

A client application can then parse this JSON to display the weather information to users.

Configuration Files

Many applications use JSON to store configuration settings. For instance, a web application might have a configuration file named config.json that stores settings for connecting to a database:

{
"database": {
"host": "localhost",
"port": 5432,
"username": "admin",
"password": "secret"
},
"logging": {
"level": "debug"
}
}

This JSON file can be read by the application during startup to configure the database connection and logging levels.

Data Storage

NoSQL databases like MongoDB use JSON-like documents to store data. For example, a user document in a MongoDB collection might look like this:

{
"_id": "507f191e810c19729de860ea",
"username": "johndoe",
"email": "john.doe@example.com",
"created_at": "2023-06-10T10:00:00Z"
}

This document format allows for flexible and dynamic schemas, which is a key advantage of NoSQL databases.

Getting Data from Third-Party Services

Many third-party services and APIs provide data in JSON format, allowing easy integration and data manipulation. For example, a stock market API might return the latest stock prices in JSON:

{
"symbol": "AAPL",
"price": 150.00,
"timestamp": "2023-06-10T10:00:00Z"
}

Applications can consume this JSON data to display stock prices or perform further analysis.

Mobile Applications

JSON is widely used in mobile applications to transmit data between the server and the client. For example, a mobile app might send a request to a server to get a list of products, and the server responds with a JSON array of products:

[
{
"id": 1,
"name": "Product 1",
"price": 19.99
},
{
"id": 2,
"name": "Product 2",
"price": 29.99
}
]

The mobile app can then parse this JSON array to display the products in a list view.

Serializing JSON with Newtonsoft.Json

Serialization is the process of converting an object into a format that can be easily stored or transmitted. Newtonsoft.Json makes it simple to serialize .NET objects into JSON strings. Let’s explore how to serialize objects using Newtonsoft.Json.

Basic Serialization

To serialize an object into a JSON string, you can use the JsonConvert.SerializeObject method. Here's a basic example:

using Newtonsoft.Json;
using System;

public class User
{
public int UserId { get; set; }
public string UserName { get; set; }
public string Email { get; set; }
}

class Program
{
static void Main()
{
User user = new User
{
UserId = 12345,
UserName = "john_doe",
Email = "john.doe@example.com"
};

string json = JsonConvert.SerializeObject(user);
Console.WriteLine(json);
}
}

In this example, the User object is serialized into a JSON string. The JsonConvert.SerializeObject method automatically converts the object's properties into JSON format.

Formatting JSON Output

You can format the JSON output to make it more readable by using the Formatting enum:

string json = JsonConvert.SerializeObject(user, Formatting.Indented);
Console.WriteLine(json);

This will produce a nicely formatted JSON string:

{
"UserId": 12345,
"UserName": "john_doe",
"Email": "john.doe@example.com"
}

Using JsonProperty Attribute

Just as with deserialization, you can use the JsonProperty attribute to control how properties are serialized:

public class User
{
[JsonProperty("user_id")]
public int UserId { get; set; }

[JsonProperty("user_name")]
public string UserName { get; set; }

[JsonProperty("email_address")]
public string Email { get; set; }
}

User user = new User
{
UserId = 12345,
UserName = "john_doe",
Email = "john.doe@example.com"
};

string json = JsonConvert.SerializeObject(user, Formatting.Indented);
Console.WriteLine(json);

This will produce:

{
"user_id": 12345,
"user_name": "john_doe",
"email_address": "john.doe@example.com"
}

Handling Null Values

You can control how null values are handled during serialization using the NullValueHandling setting:

JsonSerializerSettings settings = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
};

string json = JsonConvert.SerializeObject(user, settings);
Console.WriteLine(json);

If Email was null, it would be omitted from the JSON output.

Custom Converters

For more complex scenarios, you can create custom converters by inheriting from JsonConverter. This allows you to define custom serialization logic:

public class DateTimeConverter : JsonConverter<DateTime>
{
private readonly string _format = "yyyy-MM-dd";

public override void WriteJson(JsonWriter writer, DateTime value, JsonSerializer serializer)
{
writer.WriteValue(value.ToString(_format));
}

public override DateTime ReadJson(JsonReader reader, Type objectType, DateTime existingValue, bool hasExistingValue, JsonSerializer serializer)
{
return DateTime.ParseExact((string)reader.Value, _format, null);
}
}

You can then use this custom converter in your class:

public class Event
{
public string Name { get; set; }

[JsonConverter(typeof(DateTimeConverter))]
public DateTime Date { get; set; }
}

Event ev = new Event
{
Name = "Conference",
Date = new DateTime(2024, 6, 10)
};

string json = JsonConvert.SerializeObject(ev, Formatting.Indented);
Console.WriteLine(json);

This will produce:

{
"Name": "Conference",
"Date": "2024-06-10"
}

Deserializing JSON with Newtonsoft.Json

Deserialization is the process of converting a JSON string back into an object. Newtonsoft.Json makes it simple to deserialize JSON strings into .NET objects. Let’s explore how to deserialize JSON into objects using Newtonsoft.Json.

Basic Deserialization

To deserialize JSON into a C# object, you can use the JsonConvert.DeserializeObject<T> method. Here's a basic example:

using Newtonsoft.Json;
using System;

public class User
{
public int UserId { get; set; }
public string UserName { get; set; }
public string Email { get; set; }
}

class Program
{
static void Main()
{
string json = "{\"UserId\":12345, \"UserName\":\"john_doe\", \"Email\":\"john.doe@example.com\"}";
User user = JsonConvert.DeserializeObject<User>(json);

Console.WriteLine($"UserId: {user.UserId}, UserName: {user.UserName}, Email: {user.Email}");
}
}

In this example, the JSON string is deserialized into an instance of the User class. The JsonConvert.DeserializeObject<T> method automatically maps the JSON properties to the corresponding properties of the User object.

Using JsonProperty Attribute

In some cases, JSON property names may not match the property names in your C# class. You can use the JsonProperty attribute to map JSON properties to class properties:

public class User
{
[JsonProperty("user_id")]
public int UserId { get; set; }

[JsonProperty("user_name")]
public string UserName { get; set; }

[JsonProperty("email_address")]
public string Email { get; set; }
}

This ensures that the JSON properties are correctly mapped even if their names differ from the class properties.

Handling Missing or Extra Properties

By default, Newtonsoft.Json will ignore extra properties in the JSON that are not present in the target class. However, you can handle these scenarios explicitly using settings like MissingMemberHandling and DefaultValueHandling:

JsonSerializerSettings settings = new JsonSerializerSettings
{
MissingMemberHandling = MissingMemberHandling.Error,
DefaultValueHandling = DefaultValueHandling.Populate
};

User user = JsonConvert.DeserializeObject<User>(json, settings);

Null Value Handling

You can control how null values are handled during deserialization using the NullValueHandling setting:

JsonSerializerSettings settings = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
};

User user = JsonConvert.DeserializeObject<User>(json, settings);

Custom Converters

For more complex scenarios, you can create custom converters by inheriting from JsonConverter. This allows you to define custom serialization and deserialization logic:

public class DateTimeConverter : JsonConverter<DateTime>
{
private readonly string _format = "yyyy-MM-dd";

public override void WriteJson(JsonWriter writer, DateTime value, JsonSerializer serializer)
{
writer.WriteValue(value.ToString(_format));
}

public override DateTime ReadJson(JsonReader reader, Type objectType, DateTime existingValue, bool hasExistingValue, JsonSerializer serializer)
{
return DateTime.ParseExact((string)reader.Value, _format, null);
}
}

You can then use this custom converter in your class:

public class Event
{
public string Name { get; set; }

[JsonConverter(typeof(DateTimeConverter))]
public DateTime Date { get; set; }
}

Conclusion

Newtonsoft.Json is a powerful and flexible library for handling JSON in .NET applications. Its wide array of features and settings allows for precise control over the serialization and deserialization process, making it suitable for a variety of use cases, from simple data mapping to complex custom conversions.

Understanding how to effectively use Newtonsoft.Json can greatly enhance your ability to work with JSON data in .NET, ensuring robust and maintainable code.

--

--