简体   繁体   中英

Remove all occurrences of a particular key from a JSON response in C#

I have a JSON string from which I want to eliminate all the occurrences of a given key.

JSON I have:

string requstBody =       
{
              "payLoad": [
                {
                  "BaseVersionId_": 9,
                  "VersionId_": 10,              
                  "AssetCollateralLink": [
                    {
                      "AssetId": 137,
                      "BaseVersionId_": 9,
                      "VersionId_": 10
                    },
                    {
                      "AssetId": 136,
                      "BaseVersionId_": 0,
                      "VersionId_": 1
                    }
                  ],
                  "CollateralProvider": [],
                  "AdvCollateralAllocation": [
                    {
                      "LinkId": 91,
                      "IsDeleted_": false,
                      "BaseVersionId_": 1,
                      "VersionId_": 2
                    }
                  ]
                }
              ]
            }

I want to eliminate keys "BaseVersionID_" and "VersionId_" as follows:

  string requstBody =  
{
              "payLoad": [
                {
                  "AssetCollateralLink": [
                    {
                      "AssetId": 137
                    },
                    {
                      "AssetId": 136
                    }
                  ],
                  "CollateralProvider": [],
                  "AdvCollateralAllocation": [
                    {
                      "LinkId": 91,
                      "IsDeleted_": false
                    }
                  ]
                }
              ]
            }

I used JObject.Remove(); as follows

JObject sampleObj1 = new JObject();
sampleObj1 = JsonHelper.JsonParse(requestBody);
sampleObj1.Remove("BaseVersionId_");

but able to remove the keys under payLoad Hierarchy only. How do I remove all the occurrences of the Key.

The required properties can be removed from the Json , simply with Linq:

var jsonObj = JObject.Parse(requestBody);
jsonObj.SelectToken("payLoad[0]").SelectToken("AdvCollateralAllocation")
                .Select(jt => (JObject)jt)
                .ToList()
                .ForEach(r =>
                    r
                    .Properties()
                    .ToList()
                    .ForEach(e =>
                    {
                        if (e.Name == "BaseVersionId_" || e.Name == "VersionId_")
                            e.Remove();
                    }));

The resultant jsonObj will be without the BaseVersionId_ and VersionId_ names as well as their values.

I'd use JsonPath as such:

var toRemove =  jsonObject
                .SelectTokens("$.payLoad.[*].AssetCollateralLink.[*]..BaseVersionId_")
                .Concat(stuff.SelectTokens("$.payLoad.[*].AssetCollateralLink.[*]..VersionId_"))
                .Concat(stuff.SelectTokens("$.payLoad.[*].AdvCollateralAllocation.[*]..VersionId_"))
                .Concat(stuff.SelectTokens("$.payLoad.[*].AdvCollateralAllocation.[*]..BaseVersionId_"))
                .ToList();

for (int i = toRemove.Count - 1; i >= 0; i--)
{
    toRemove[i].Parent?.Remove();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM