Jsonmasking
Replace fields in json, replacing by something, don't care if property is in depth objects. Very useful to replace passwords credit card number, etc.
Install / Use
/learn @ThiagoBarradas/JsonmaskingREADME
Json Masking
Replace fields in json, replacing by something, don't care if property is in depth objects. Very useful to replace passwords, credit card number, etc.
This library matching insensitive values with field namespaces. You can use wildcard * to allow any char in pattern;
Sample
- Completely masking some of the properties
var example = new
{
SomeValue = "Demo",
Password = "SomePasswordHere",
DepthObject = new
{
Password = "SomePasswordHere2",
Card = new
{
Number = "555500022223333"
}
},
CreditCardNumber = "5555000011112222",
Card = new
{
Number = "555500022223333"
}
};
var exampleAsString = JsonConvert.Serialize(example); // value must be a json string to masked
// note that password is only replaced when is in root path
var blacklist = new string[] { "password", "card.number", "*.card.number" "creditcardnumber" };
var mask = "******";
var maskedExampleAsString = exampleAsString.MaskFields(blacklist, mask);
Console.WriteLine(maskedExampleAsString);
Output
{
"SomeValue" : "Demo",
"Password" : "******",
"DepthObject" : {
"Password" : "SomePasswordHere2",
"Card" : {
"Number" : "******"
}
},
"CreditCardNumber" : "******",
"Card" : {
"Number" : "******"
}
}
- Partially masking some of the properties
var example = new
{
SomeValue = "Demo",
Password = "SomePasswordHere",
DepthObject = new
{
Password = "SomePasswordHere2",
Card = new
{
Number = "555500022223333"
}
},
CreditCardNumber = "5555000011112222",
Card = new
{
Number = "555500022223333"
}
};
var blacklistPartial = new Dictionary<string, Func<string, string>>(StringComparer.OrdinalIgnoreCase) // The key is the property to be partially masked and the value is the function to be applied.
{
{ "*card.number", text => // Note that the property "*card.number" is also in the blacklist. If the property only exists in the blacklistPartial, it will not be masked.
Regex.Replace(
text,
@"(\d{4,5})[ -|]?(\d{3,6})[ -|]?(\d{3,5})[ -|]?(\d{3,4})",
match => $"{match.Value.Substring(0, 6)}*****{match.Value.Substring(match.Value.Length - 4, 4)}")
}
};
var exampleAsString = JsonConvert.Serialize(example); // The value must be a JSON string to be masked.
// Note that the password is only replaced when it is in the root path.
var blacklist = new string[] { "password", "*card.number", "creditcardnumber" };
var mask = "******";
var maskedExampleAsString = exampleAsString.MaskFields(blacklist, mask, blacklistPartial); // The blacklistPartial is optional. If provided, it will apply the mask only if the property is also in the blacklist.
Console.WriteLine(maskedExampleAsString);
Output
{
"SomeValue" : "Demo",
"Password" : "******",
"DepthObject" : {
"Password" : "SomePasswordHere2",
"Card" : {
"Number" : "555500*****3333"
}
},
"CreditCardNumber" : "******",
"Card" : {
"Number" : "555500*****3333"
}
}
Install via NuGet
PM> Install-Package JsonMasking
How can I contribute?
Please, refer to CONTRIBUTING
Found something strange or need a new feature?
Open a new Issue following our issue template ISSUE_TEMPLATE
Changelog
See in nuget version history
Did you like it? Please, make a donate :)
if you liked this project, please make a contribution and help to keep this and other initiatives, send me some Satochis.
BTC Wallet: 1G535x1rYdMo9CNdTGK3eG6XJddBHdaqfX

Related Skills
node-connect
347.6kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
108.4kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
347.6kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
347.6kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
