Skip to content

Instantly share code, notes, and snippets.

@samsalisbury
Created December 19, 2013 17:29
Show Gist options
  • Save samsalisbury/8043044 to your computer and use it in GitHub Desktop.
Save samsalisbury/8043044 to your computer and use it in GitHub Desktop.
Anonymise certain fields in a blob of JSON using Json.Net + JToken
using System.Linq;
using Newtonsoft.Json.Linq;
namespace ExternalApi.Logging
{
public static class UserInputAnonymiser
{
public static JToken Anonymise(JToken data)
{
foreach (var prop in data.Children<JProperty>())
{
if (ShouldBeAnonymised(prop.Name))
prop.Value = "-{anonymised}-";
else if (prop.Value.HasValues)
prop.Value = Anonymise(prop.Value);
}
return data;
}
private readonly static string[] AnonymousProps = { "firstname", "lastname", "emailaddress", "phonenumber" };
private static bool ShouldBeAnonymised(string propertyName)
{
return AnonymousProps.Any(x => propertyName.ToLowerInvariant().Contains(x));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment