how to remove child of a parent from json string based on condition in c# or linq
-
hi I have a scenario where I wanted to remove children of a parent. here is my json object. I want to remove the last CP not parent of CP. appreciate your help.
Object
->Address
->PRule
->0
->RuleP
->CP
->0
->CP -
That is far from clear, both question and diagram. Please edit your question and clarify the exact structure and what the problem is.
json string have parent and child and child has sub child(grand child). Here wanted to remove grand child. child name and grand child name is same so if i remove based on the name it will remove child and grand child. so if grand child has child as parent then remove grand child. I have converted json string to JObject here my code snippet: i know this is wrong it deletes both child and grand child because names are same. jObject.Descendants() .OfType() .Where(attr => attr.Name.Contains("GrandChildName")) .ToList() .ForEach(attr => attr.Remove());
-
json string have parent and child and child has sub child(grand child). Here wanted to remove grand child. child name and grand child name is same so if i remove based on the name it will remove child and grand child. so if grand child has child as parent then remove grand child. I have converted json string to JObject here my code snippet: i know this is wrong it deletes both child and grand child because names are same. jObject.Descendants() .OfType() .Where(attr => attr.Name.Contains("GrandChildName")) .ToList() .ForEach(attr => attr.Remove());
Yes, because you are doing exactly what you have previously explained that you must not do. You need to create the logic to step through the objects and find the grandchild entry in the structure of the data. That is, not by searching for the name, but by looking at the relationship between objects, or their position.
-
Yes, because you are doing exactly what you have previously explained that you must not do. You need to create the logic to step through the objects and find the grandchild entry in the structure of the data. That is, not by searching for the name, but by looking at the relationship between objects, or their position.
Yes I trying to remove if parent of child name also same but below solution is not workin. Here is my full code, first converted my c# class list object to json string, and then to JObject.
var jsonString = JsonConvert.SerializeObject(eligibilityModel);
JObject jObject = JObject.Parse(jsonString);
jObject.Descendants()
.OfType()
.Where(attr => attr.Name.Contains("ChildProducts") && attr.Parent.Equals("ChildProducts" ))
.ToList()
.ForEach(attr => attr.Remove()); -
Yes I trying to remove if parent of child name also same but below solution is not workin. Here is my full code, first converted my c# class list object to json string, and then to JObject.
var jsonString = JsonConvert.SerializeObject(eligibilityModel);
JObject jObject = JObject.Parse(jsonString);
jObject.Descendants()
.OfType()
.Where(attr => attr.Name.Contains("ChildProducts") && attr.Parent.Equals("ChildProducts" ))
.ToList()
.ForEach(attr => attr.Remove());