How to remove a property from Json in C#

|
| By Webner

Description: Suppose we have the following JSON data and we want to remove the property “RunIsolated” (highlighted) under the compositeRequest to get the final JSON without this property:
code

Solution for Json:

Following is the sample code to achieve this:
string jsonContent = "{\"allOrNone\":false,\"compositeRequest\":[{\"method\":\"PATCH\",\"referenceId\":\"REF09095303b88a40708d737c10cdc01107\",\"body\":{\"RunIsolated\":false,\"XXXX__Account_Manager__c\":1}},{\"method\":\"PATCH\",\"referenceId\":\"REF1899153fa1964e7ea3d3efb523005724\",\"body\":{\"RunIsolated\":false,\"XXXX__Account_Manager__c\":2}},{\"method\":\"PATCH\",\"referenceId\":\"REF1899153fa1964e7ea3d3efb523005724\",\"body\":{\"RunIsolated\":false,\"XXXX__Account_Manager__c\":3}}]}";
dynamic jObj = JsonConvert.DeserializeObject(jsonContent);
foreach (var item in jObj.compositeRequest)
{
JObject header = (JObject)item.SelectToken("body");
header.Property(“RunIsolated”).Remove();
}
string json = jObj.ToString();

code1
Final Output: From the screenshot, it can be seen, the property “RunIsolated” is removed in the final JSON now.
Json

Leave a Reply

Your email address will not be published. Required fields are marked *