See Microsoft Learn Ignore individual properties which shows several variants code samples also.
Edit
See if this works for you
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace DeserializeAvoidNullConsoleApp
{
class Program
{
private static string _fileName = "data.json";
static void Main(string[] args)
{
Write();
var list = Read();
foreach (var data in list)
{
Console.WriteLine($"{data.first_name, -10}{data.country, -10} {(data.country is null)}");
}
Console.ReadLine();
}
private static List<SigninData> Read()
=> JsonSerializer.Deserialize<List<SigninData>>(File.ReadAllText(_fileName));
private static void Write()
{
List<SigninData> signinData = new()
{
new () { country = null, first_name = "Bob", last_name = "Smith", email_address = "bob@gmail", user_id = "A1", user_uuid = "Whatever" },
new () { country = "Germany", first_name = "Anne", last_name = "Smith", email_address = "A@gmail", user_id = "B1", user_uuid = "XXXX" }
};
var options = JsonSerializerOptions();
File.WriteAllText(_fileName, JsonSerializer.Serialize(signinData, options));
}
private static JsonSerializerOptions JsonSerializerOptions() =>
new ()
{
WriteIndented = true,
Converters = { new NullToEmptyStringConverter() }
};
}
class SigninData
{
public string user_id { get; set; }
public string user_uuid { get; set; }
public string first_name { get; set; }
public string last_name { get; set; }
public string email_address { get; set; }
public string country { get; set; }
}
public class NullToEmptyStringConverter : JsonConverter<string>
{
public override bool HandleNull => true;
public override bool CanConvert(Type typeToConvert)
=> typeToConvert == typeof(string);
public override string Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> throw new NotImplementedException();
public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options)
{
writer.WriteStringValue(value ?? "");
}
}
}
Json
[
{
"user_id": "A1",
"user_uuid": "Whatever",
"first_name": "Bob",
"last_name": "Smith",
"email_address": "bob@gmail",
"country": ""
},
{
"user_id": "B1",
"user_uuid": "XXXX",
"first_name": "Anne",
"last_name": "Smith",
"email_address": "A@gmail",
"country": "Germany"
}
]