-2

I have a JSON as shown in below format:

{
  "type": "AdaptiveCard",
  "$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
  "version": "1.3",
  "msteams": {
    "width": "Full"
  },
  "body": [
    {
      "type": "TextBlock",
      "text": "Select from below",
      "wrap": true
    },
    {
      "type": "TextBlock",
      "text": "  - **Mango**: Seasonal fruit available in summers and king of fruits.\n  - **Apple**: available always and is considered to keep doctors away if you have them daily",
      "wrap": true
    },
    {
      "type": "ActionSet",
      "actions": [
        {
          "type": "Action.ShowCard",
          "title": "Mango",
          "card": {
            "type": "AdaptiveCard",
            "body": [
              {
                "type": "ColumnSet",
                "columns": [
                  {
                    "type": "Column",
                    "width": "stretch",
                    "items": [
                      {
                        "type": "Input.ChoiceSet",
                        "id": "staticID",
                        "isRequired": true,
                        "errorMessage": "Select a code to proceed",
                        "label": "Select a code:",
                        "choices": [
                            {
                            "title": "AE",
                            "value": "AE"
                          },
                          {
                            "title": "APPS1",
                            "value": "APPS1"
                          }
                        ],
                        "placeholder": "Select one"
                      }
                    ]
                  }
                ]
              },
              {
                "type": "ColumnSet",
                "columns": [
                  {
                    "type": "Column",
                    "width": "stretch",
                    "verticalContentAlignment": "Top",
                    "horizontalAlignment": "Left",
                    "items": [
                      {
                        "type": "Input.Text",
                        "placeholder": "Enter comments",
                        "id": "staticdlname",
                        "label": "Please enter your mango stories",
                        "isRequired": true,
                        "errorMessage": "Enter comment"
                      }
                    ]
                  }
                ]
              },
              {
                "type": "ActionSet",
                "actions": [
                  {
                    "type": "Action.Submit",
                    "title": "Submit",
                    "data": {
                      "value": "staticdl"
                    }
                  }
                ]
              }
            ]
          }
        },
        {
          "type": "Action.ShowCard",
          "title": "Dynamic",
          "card": {
            "type": "AdaptiveCard",
            "body": [
              {
                "type": "ColumnSet",
                "columns": [
                  {
                    "type": "Column",
                    "width": "stretch",
                    "items": [
                      {
                        "type": "Input.ChoiceSet",
                        "id": "dynamicid",
                        "isRequired": true,
                        "errorMessage": "Select a code proceed",
                        "label": "Select a code:",
                        "choices": [
                            {
                            "title": "APAC",
                            "value": "APAC"
                          },
                          {
                            "title": "EU",
                            "value": "EU"
                          }
                        ],
                        "placeholder": "Select one"
                      }
                    ]
                  }
                ]
              },
              {
                "type": "ColumnSet",
                "columns": [
                  {
                    "type": "Column",
                    "width": "stretch",
                    "verticalContentAlignment": "Top",
                    "horizontalAlignment": "Left",
                    "items": [
                      {
                        "type": "Input.Text",
                        "placeholder": "Enter your apple stories",
                        "id": "dynamicdlname",
                        "label": "Enter a comment",
                        "isRequired": true,
                        "errorMessage": "Enter a comment"
                      }
                    ]
                  }
                ]
              },
              {
                "type": "ActionSet",
                "actions": [
                  {
                    "type": "Action.Submit",
                    "title": "Submit",
                    "data": {
                      "value": "dynamicdl"
                    }
                  }
                ]
              }
            ]
          }
        }
      ]
    }
  ]
}

I would like to remove the entire node of "ActionSet" if the condition satisfies that type = "Action.Submit".

I tried below code:

var jtok = JToken.Parse(json);
var result = jtok["body"].Select(x => x["actions"]).ToList();
foreach(var item in result)
{
if(item != null)
   {
     item.Parent.Remove();
   }
}
var output = jtok.ToString(Newtonsoft.Json.Formatting.Indented);

But I got error: "The parent is missing." This is the updated json which contains different types Action. From this i just need to delete nodes which contain Action.Submit.

SDR
  • 37
  • 1
  • 1
  • 8
  • 1
    Break it he code down into individual statements to see where the error is. – stuartd Apr 12 '23 at 18:09
  • Since ActionSet can have multiple actions, are you removing the ActionSet if _any_ of its actions have type=Action.Submit, or only if _all_ of them do? If the action types are mixed, would you want to remove actions with type=Action.Submit from the actions array, while leaving the ActionSet itself in place? – StriplingWarrior Apr 12 '23 at 18:18
  • @StriplingWarrior If action type is mixed i want to remove actions with type=Action.Submit from the actions array, while leaving the ActionSet itself in place but if it contains type=Action.Submit and no other type, i want to remove the whole ActionSet. – SDR Apr 12 '23 at 18:33

1 Answers1

2

try this, I don't know how many objects to remove can contain your body, so it will remove all of them

    var jObj = JObject.Parse(json);
    var toRemove = jObj["body"].Where(x => (string)x["type"] == "ActionSet")
                                .SelectMany(y => y["actions"]
                                .Where(y => (string)y["type"] == "Action.Submit")
                                ).ToArray();

    for (int i = 0; i < toRemove.Count(); i++)
        toRemove[i].Remove();

        // check if there are empty "actions" arrays
    var itemsToRemove = jObj["body"].Where(x => (string)x["type"] == "ActionSet"
                                        && x["actions"].Count() == 0).ToArray();

    for (int i = 0; i < itemsToRemove.Count(); i++)
        itemsToRemove[i].Remove();

    json = jObj.ToString();

but since you have not decided yet what do you want, maybe this will work for you too

 var itemsToRemove = jObj["body"].Where(x => (string)x["type"] =="ActionSet"
                                && x["actions"].Any(y => (string)y["type"] == "Action.Submit")
                                ).ToArray();

    for (int i = 0; i < itemsToRemove.Count(); i++)
        itemsToRemove[i].Remove(); 

UPDATE

this is just a hint for your multi-level json string

var toRemove = jObj["body"].Where(x => (string)x["type"] == "ActionSet").SelectMany(x => x["actions"])
    .SelectMany(x => x["card"]["body"]).Where(x => (string)x["type"] == "ActionSet").SelectMany(x => x["actions"])
    .Where(y => (string)y["type"] == "Action.Submit").ToArray();

    for (int i = 0; i < toRemove.Count(); i++)
    {
        toRemove[i].Parent.Parent.Parent.Remove();
    }
Serge
  • 40,935
  • 4
  • 18
  • 45
  • Your code works if all the types are Action.Submit, but it fails when there are other types as well inside ActionSet. For example it doesn't work for the below JSOn: ``` – SDR Apr 12 '23 at 20:23
  • 1
    @SDR I don't what json you tested. It works with json you posted. If your json is different, you should not waste our time but should post a real json. – Serge Apr 12 '23 at 20:32
  • I added the json for which its failing. Can you test this json please. I didn't intend to waste your time. I am trying to create a generic function which works for all the types. – SDR Apr 12 '23 at 20:39
  • What result do you need ? W:hat code works for you 1st or 2nd. In one place you ask the one, in another completely opposite one – Serge Apr 12 '23 at 20:44
  • If action type is mixed i want to remove actions with type=Action.Submit from the actions array, while leaving the ActionSet itself in place but if it contains type=Action.Submit and no other type, i want to remove the whole ActionSet. – SDR Apr 12 '23 at 20:50
  • @SDR This is what I am talking about the waste our time. Code for this question is too big for stack overflow. Nobody will work as an free coder for you. – Serge Apr 13 '23 at 02:08