using System.Collections.Generic;
using Newtonsoft.Json.Linq;
public static void Main()
var json = @"{'name':'doug','things_done':['do','not','remove','these','ComplyAdvantage'],'diligence':{'watchlists':{'matches':['a','b','c']}},'raw_responses':{'a':'b','ComplyAdvantage':'bye bye'},'formatted_responses':{'ComplyAdvantage':'bye','a':'b'},'supplied':{'a':'b'}, 'test':{'supplied':'this is wanted','ComplyAdvantage':'this is not?','matches':['should','exist'],'noneOfTheAbove':'w00t'}}";
var filteredJson = FilterRawData(json);
Console.WriteLine(filteredJson);
var moreFilteredJson = FilterRawDataV2(json);
Console.WriteLine(moreFilteredJson);
private static string FilterRawData(string rawData)
var jsonObj = JObject.Parse(rawData);
jsonObj.SelectToken("supplied")?.Parent.Remove();
jsonObj.SelectToken("diligence.watchlists.matches")?.Parent.Remove();
jsonObj.SelectToken("raw_responses.ComplyAdvantage")?.Parent.Remove();
jsonObj.SelectToken("formatted_responses.ComplyAdvantage")?.Parent.Remove();
return jsonObj.ToString(Formatting.None);
private static string FilterRawDataV2(string rawData)
var jsonObj = JObject.Parse(rawData);
RemoveFields(jsonObj, new[] { "supplied", "matches", "ComplyAdvantage" });
return jsonObj.ToString(Formatting.None);
public static JToken RemoveFields(JToken token, string[] fields)
JContainer container = token as JContainer;
if (container == null) return token;
List<JToken> removeList = new List<JToken>();
foreach (JToken el in container.Children())
JProperty p = el as JProperty;
if (p != null && fields.Contains(p.Name))
RemoveFields(el, fields);
foreach (JToken el in removeList)