I'm looking for a way to find a specific json value by its name and set its value to null. The construction of the json file can be anything, it's not always the same.
Let's say the json looks like this:
[
{
"id": "1111",
"email": "[email protected]",
},
{
"id": "2222",
"email": "[email protected]",
}]
The result I'm looking for is this:
[
{
"id": "1111",
"email": null,
},
{
"id": "2222",
"email": null,
}]
When the object is more complicated it should work too.
{
"reservations": [
{
"id": "111",
"bookingId": "",
"status": "",
"checkInTime": "",
"checkOutTime": "",
"property": {
"id": "",
"code": "",
"name": "",
},
"primaryGuest": {
"firstName": "",
"middleInitial": "",
"lastName": "",
"email": "[email protected]",
"phone": "",
"address": {
"addressLine1": "",
"postalCode": "",
"city": "",
"countryCode": ""
}
},
"booker": {
"firstName": "",
"middleInitial": "",
"lastName": "",
"email": "[email protected]",
"phone": ""
}
}]}
I've tried to use JArray, JObject classes etc, but it only works if the propety["email"] is the first child, not deeper. Not sure how to accomplish this.
private JObject HashSensitiveData(JContainer jContainer)
{
if (!jContainer.Descendants().Any())
{
return null;
}
var objects = jContainer.Descendants().OfType<JObject>();
foreach (var property in objects)
{
foreach (var emailProperty in property.Properties().Where(x => x.Name.CaseInsensitiveContains(LoggerHashedProperties.Email.ToString())))
{
var email = emailProperty.Value.ToString();
property[emailProperty.Name] =null
}
}
return HashSensitiveData(jContainer);
}