How to serialize properties to lower case using System.Text.Json.JsonSerializer

Andrus 121 Reputation points
2021-10-09T09:24:11.907+00:00

ASP.NET 5 MVC Core application controller consumes json API using

  using System.Text.Json;

    public async Task<IActionResult> EstoAPICall()
    {
  ...
  EstoOst estoOst;
  ....
  var json = JsonSerializer.Serialize(estoOst);
  StringContent content = new(json, Encoding.UTF8, "application/json");
  using var response = await httpClient.PostAsync("https://example.com", content);
  }

  public class EstoOst
  {
    public decimal Amount { get; set; }
    }

This returns error response because caller requires lower case amount but Serialize method returns upper case Amount.

How to fix this ? Switching to Newtosoft or changing class property name to lowercase seems to be not good solutions.

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,165 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,254 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. AgaveJoe 26,201 Reputation points
    2021-10-09T12:27:58.89+00:00

    The official documentation illustrates how to control casing. For example, camel casing.

    How to customize property names and values with System.Text.Json

    0 comments No comments