---
title: "How to update the option for JSON serialization."  
description: "How to update the option for JSON serialization."  
author: "Utpal Vishwas"  
published: 2023-11-07  
updated: 2023-11-08  
canonical: https://www.mindstick.com/forum/160459/how-to-update-the-option-for-json-serialization  
category: ".net core"  
tags: ["asp.net core", ".net core", ".net core 6"]  
reading_time: 3 minutes  

---

# How to update the option for JSON serialization.

How to [update](https://www.mindstick.com/forum/156353/what-is-hummingbird-update) the [option](https://www.mindstick.com/forum/159391/jquery-get-selected-option-from-dropdown) for [JSON](https://www.mindstick.com/forum/34446/convert-json-string-to-object) serialization.

## Replies

### Reply by Aryan Kumar

In .NET Core 6, you can update the options for JSON serialization using the **JsonSerializerOptions** class. These options allow you to control various aspects of JSON serialization and deserialization. Here's how you can update the options for JSON serialization:

**Configure JSON Serialization Options**:

In your ASP.NET Core application's **Startup.cs** or wherever you configure JSON serialization, you can create an instance of **JsonSerializerOptions** and update its properties. For example:

csharpCopy code

```plaintext
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        // Add JSON serialization with custom options
        services.AddControllers()
            .AddJsonOptions(options =>
            {
                options.JsonSerializerOptions.PropertyNamingPolicy = null; // Use default casing
                options.JsonSerializerOptions.WriteIndented = true; // Pretty-print JSON
                // Add other configuration options as needed
            });
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            // Production error handling
            app.UseExceptionHandler("/Home/Error");
            app.UseHsts();
        }

        // Add middleware and routing

        app.UseHttpsRedirection();
        app.UseStaticFiles();

        app.UseRouting();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
    }
}
```

In this example, we've configured JSON serialization options such as the property naming policy and pretty-printing of JSON. You can update these options based on your specific requirements.

**Apply Options to Specific Controllers or Actions**:

If you want to apply specific JSON serialization options to a particular controller or action method, you can use the **[JsonOptions]** attribute. For example:

```plaintext
[ApiController]
[Route("api/[controller]")]
public class MyController : ControllerBase
{
    [HttpGet]
    [JsonOptions(WriteIndented = true)]
    public IActionResult Get()
    {
        var data = new { Name = "John", Age = 30 };
        return Ok(data);
    }
}
```

In this example, the **WriteIndented** option is applied only to the **Get** action method.

**Configure Default Options for the Entire Application**:

You can also configure default JSON serialization options for the entire application in the **Program.cs** file when building the web host. Here's an example:

```plaintext
using Microsoft.AspNetCore.Hosting;
using System.Text.Json;

public class Program
{
    public static void Main(string[] args)
    {
        var host = WebApplication.CreateBuilder(args)
            .ConfigureAppConfiguration((hostingContext, config) =>
            {
                // Add configuration settings if needed
            })
            .ConfigureServices((hostingContext, services) =>
            {
                // Add services and configure JSON serialization
                services.AddControllers()
                    .AddJsonOptions(options =>
                    {
                        options.JsonSerializerOptions.Write
```

In this example, we configure default JSON serialization options for the entire application.

By updating the **JsonSerializerOptions** with the desired settings, you can control various aspects of JSON serialization in your ASP.NET Core 6 application, including property naming, formatting, and more.


---

Original Source: https://www.mindstick.com/forum/160459/how-to-update-the-option-for-json-serialization

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
