1

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);
    }
2
  • "The construction of the json file can be anything, it's not always the same." How are you going to use it if you even don't know the structure? Please post the real code Commented Aug 15, 2021 at 0:14
  • The structure is not constant which means the email node can be nested. I'm using it for logging - I log the api request response I receive as json and need to set all the value of a node email to null. Commented Aug 15, 2021 at 7:26

2 Answers 2

4

Using NewtonSoft, I once made a flatten function to analyse json files of any depth:

IEnumerable<JProperty> Flatten(JToken token)
{
    return token.Children<JProperty>().Concat(
        token.Children().SelectMany(t => Flatten(t)))
        .Where(t => t.Value is JValue);
}

It returns a flat listing of all "endpoint" JsonPropertys in a file (say: all "xyx" : primitive value entries). Using it you can simply deserialize your Json, find all "email" properties and set their value to null:

var jobj = JsonConvert.DeserializeObject<JObject>(getJson());

var flattened = Flatten(jobj);

foreach (var jprop in flattened.Where(t => t.Name == "email"))
{
    jprop.Value = null;
}
var json = JsonConvert.SerializeObject(jobj).Dump();

Result for the "more complicated" json (with one deeper email added to make it more fun):

{
  "reservations": [
    {
      "id": "111",
      "bookingId": "",
      "status": "",
      "checkInTime": "",
      "checkOutTime": "",
      "property": {
        "id": "",
        "code": "",
        "name": ""
      },
      "primaryGuest": {
        "firstName": "",
        "middleInitial": "",
        "lastName": "",
        "email": null,
        "phone": "",
        "address": {
          "email": null,
          "addressLine1": "",
          "postalCode": "",
          "city": "",
          "countryCode": ""
        }
      },
      "booker": {
        "firstName": "",
        "middleInitial": "",
        "lastName": "",
        "email": null,
        "phone": ""
      }
    }
  ]
}
Sign up to request clarification or add additional context in comments.

1 Comment

Exactly what I was looking for, thank you.
3

With System.Text.Json and Utf8JsonWriter you can process and write your JSON recursively:

public static void RemoveContent(JsonElement element, Utf8JsonWriter writer)
{
    // Current element is an array, so we have to iterate over all elements.
    if (element.ValueKind == JsonValueKind.Array)
    {
        writer.WriteStartArray();
        foreach (var e in element.EnumerateArray())
        {
            RemoveContent(e, writer);
        }
        writer.WriteEndArray();
    }
    // Current element is an object, so we have to process all properties.
    else if (element.ValueKind == JsonValueKind.Object)
    {
        writer.WriteStartObject();
        foreach (var e in element.EnumerateObject())
        {
            writer.WritePropertyName(e.Name);

            // * Process specific elements. ************************************************
            if (e.Name == "email") { writer.WriteNullValue(); }
            else RemoveContent(e.Value, writer);
        }
        writer.WriteEndObject();
    }
    // We are at the leaf (a string property, ...) and we write this as it is.
    else
    {
        element.WriteTo(writer);
    }
}

Usage:

using var stream = new MemoryStream();
// We have to flush writer before reading it's content.
using (var writer = new Utf8JsonWriter(stream, new JsonWriterOptions { SkipValidation = true }))
{
    var element = JsonDocument.Parse(jsonStr).RootElement;
    RemoveContent(element, writer);
}
// Stream contains a byte array with UTF-8 content.
// If you want a string, you can use UTF8 decoding.
var json = System.Text.Encoding.UTF8.GetString(stream.ToArray());
Console.WriteLine(json);

1 Comment

Interesting solution. I wonder if it's less resource-demanding, so maybe a solution for very large JSON files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.