I have a service that receives some data with random structure in JSON format and persists these data in a document-based database. I have no control over sending services and there are some sensitive data that should not be persisted in my database. I just want to filter those fields and replace their values with some masking characters. Is there any built-in solution in dotnet5 without adding third-party libraries like json.net or Json Newtonsoft?
I cannot deserialize incoming JSON to a static typed class because I have no idea about how many different types are sent from other services. An also working with reflection and dynamic typing has its own performance penalties.
CodePudding user response:
if you don't have any way to change in code, you can handle it in sql at the time of json insert. like:
CREATE PROCEDURE dbo.InsertJson(@doc NVARCHAR(MAX))
AS BEGIN
INSERT INTO Person (id, name, surname, age)
SELECT id, firstNAme, lastName, age
FROM OPENJSON(@json)
WITH (id int, firstName nvarchar(50), lastName nvarchar(50),age int)
END
CodePudding user response:
I finally used regex to find and make masked using this code snipped.
private string GetMaskedJson(string json, string[] fieldNames)
{
if (string.IsNullOrEmpty(json)) return string.Empty;
foreach (var field in fieldNames)
{
var pattern = "\"" field "\"\\s*:\\s*(\"?)(. ?)((\")|\\,|\\})";
var maskedValue = "===MASKED VALUE===";
var r = new Regex(pattern);
var k = r.GetGroupNames();
var t = Regex.Matches(json, pattern);
//R
foreach (var ma in t.Reverse().ToList())
{
var masked = maskedValue;
if (string.IsNullOrWhiteSpace(ma.Groups[1].Value)) masked = $"\"{maskedValue}\"";
json = json.Remove(ma.Groups[2].Index, ma.Groups[2].Length)
.Insert(ma.Groups[2].Index, masked);
}
}
return json;
}