Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. how to remove child of a parent from json string based on condition in c# or linq

how to remove child of a parent from json string based on condition in c# or linq

Scheduled Pinned Locked Moved LINQ
csharplinqjsonhelptutorial
6 Posts 2 Posters 36 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    chaks nag
    wrote on last edited by
    #1

    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

    L 1 Reply Last reply
    0
    • C chaks nag

      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

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      That is far from clear, both question and diagram. Please edit your question and clarify the exact structure and what the problem is.

      C 1 Reply Last reply
      0
      • L Lost User

        That is far from clear, both question and diagram. Please edit your question and clarify the exact structure and what the problem is.

        C Offline
        C Offline
        chaks nag
        wrote on last edited by
        #3

        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());

        L 1 Reply Last reply
        0
        • C chaks nag

          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());

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          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.

          C 1 Reply Last reply
          0
          • L Lost User

            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.

            C Offline
            C Offline
            chaks nag
            wrote on last edited by
            #5

            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());

            L 1 Reply Last reply
            0
            • C chaks nag

              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());

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              You need to use your debugger to see what is added to your List and why it includes the parent object.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups