For those not wanting to use the Newtonsoft.Json Package and instead want to use the System.Text.Json.Serialization Package, you can alternatively use this:
[JsonPropertyName("foo")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? Foo { get; set; }