I have a json string that has nested values:
{
"card": {
"number": "411111111111111111",
"expirationMonth": null,
"expirationYear": null,
"securityCode": null
}
}
I want to remove expirationMonth, expirationYear, securityCode so that only card: number remain. but I can’t find any example of how to do this with system.text.json that aren’t wickedly complex and involve basically writing your own extension library.
Here is what I’ve tried:
JsonObject requestJson = JsonSerializer.Deserialize<JsonObject>(jsonString);
requestJson.Remove("expirationMonth");
requestJson.Remove("expirationYear");
requestJson.Remove("securityCode");
But since the elements are nested JsonObject.Remove does nothing.
2
Answers
I wrote with 2 ways
Use system.text.json
use Newtonsoft.Json
you forgot about a root property. should be