Remove specific properties from JSON object

Solution 1:

You can parse the string first:

var temp =  JArray.Parse(json);
temp.Descendants()
    .OfType<JProperty>()
    .Where(attr => attr.Name.StartsWith("_umb_"))
    .ToList() // you should call ToList because you're about to changing the result, which is not possible if it is IEnumerable
    .ForEach(attr => attr.Remove()); // removing unwanted attributes
json = temp.ToString(); // backing result to json

UPDATE OR:

result.Properties()
    .Where(attr => attr.Name.StartsWith("_umb_"))
    .ToList()
    .ForEach(attr => attr.Remove());

UPDATE #2

You can specify more conditions in where clause:

.Where(attr => attr.Name.StartsWith("_umb_") && some_other_condition)

OR

.Where(attr => attr.Name.StartsWith("_umb_") || some_other_condition)

Or whatever you need.

Solution 2:

In case JArray.Parse is not working, you can strongly type your object as follows:

var temp = (Newtonsoft.Json.Linq.JObject)JsonConvert.DeserializeObject(json);
temp.Descendants()
 .OfType<JProperty>()
 .Where(attr => attr.Name.StartsWith("_umb_"))
 .ToList() // you should call ToList because you're about to changing the result, which is not possible if it is IEnumerable
 .ForEach(attr => attr.Remove()); // removing unwanted attributes