Reputation: 335
I have this JSON like this:
{
"flight" : [
{
"arv" : {
"@aptCode" : "JFK"
},
"legs" : { "@count" : "1",
"leg" : { "@arvDayIndicator" : "0",
"@wetLease" : "False",
"mealCodes" : "M M M M M M M M M M M M M M M M M M M M M M",
"operation" : { "@acftOwner" : "",
"@operatedBy" : ""
}
}
}
},
{
"arv" : { "@aptCode" : "LHR" },
"legs" : { "@count" : "2",
"leg" : { "@arvDayIndicator" : "0",
"@wetLease" : "False",
"mealCodes" : "M M M M M M M M M M M M M f M M f M M M f M",
"operation" : { "@acftOwner" : "",
"@operatedBy" : ""
}
}
}
}]}
How can remove the inner values like :
"@wetLease","mealCodes" or "@operatedBy"
I try with:
jSon.SelectToken(@"flight[0].arv").Remove();
for first instance but give me the next error:
Cannot add or remove items from Newtonsoft.Json.Linq.JProperty
Upvotes: 1
Views: 3972
Reputation: 116118
var names = new List<string>(){"@wetLease","mealCodes" , "@operatedBy"};
var jObj = JObject.Parse(json);
jObj.Descendants().OfType<JProperty>()
.Where(p=>names.Contains(p.Name))
.ToList()
.ForEach(p=>p.Remove());
var newjson = jObj.ToString();
Upvotes: 3