delete a property from large JSON, c#

Viewed 326

I am receiving JSON where I need to delete one property "original_0" form all the responses. I am working on c#. I am not sure how I can delete the property.

Below is JSON

{
"entity": [{
        "JobId": 725454,
        "Operation Type": "Backup",
        "Client": "sgkcovldidbdev01",
        "Agent": "Oracle Database",
        "Instance": "INFSGDEV",
        "BackupSet": "default",
        "Subclient": "ArchiveLog",
        " Backup Type": "Full",
        "Start Time": "2020-08-21T18:30:19.000",
        "End Time": "2020-08-21T18:32:14.000",
        "Duration in Mins": 2,
        "Size of Application": 56623104,
        "Job Status": "Completed",
        "dt": "2020-08-21T00:00:00.000",
        "age": 0,
        "original_0": {
            "numberOfDays": "2",
            "serverName": "sgkcovldidbdev01"
        }
    },
    {
        "JobId": 724699,
        "Operation Type": "Backup",
        "Client": "sgkcovldidbdev01",
        "Agent": "Oracle Database",
        "Instance": "INFSGDEV",
        "BackupSet": "default",
        "Subclient": "ArchiveLog",
        " Backup Type": "Full",
        "Start Time": "2020-08-21T12:30:09.000",
        "End Time": "2020-08-21T12:31:09.000",
        "Duration in Mins": 1,
        "Size of Application": 95420416,
        "Job Status": "Completed",
        "dt": "2020-08-21T00:00:00.000",
        "age": 0,
        "original_0": {
            "numberOfDays": "2",
            "serverName": "sgkcovldidbdev01"
        }
    },
    {
        "JobId": 724056,
        "Operation Type": "Backup",
        "Client": "sgkcovldidbdev01",
        "Agent": "Oracle Database",
        "Instance": "INFSGDEV",
        "BackupSet": "default",
        "Subclient": "ArchiveLog",
        " Backup Type": "Full",
        "Start Time": "2020-08-21T06:30:11.000",
        "End Time": "2020-08-21T06:31:10.000",
        "Duration in Mins": 1,
        "Size of Application": 333447168,
        "Job Status": "Completed",
        "dt": "2020-08-21T00:00:00.000",
        "age": 0,
        "original_0": {
            "numberOfDays": "2",
            "serverName": "sgkcovldidbdev01"
        }
    },
    {
        "JobId": 723035,
        "Operation Type": "Backup",
        "Client": "sgkcovldidbdev01",
        "Agent": "Oracle Database",
        "Instance": "INFSGDEV",
        "BackupSet": "default",
        "Subclient": "ArchiveLog",
        " Backup Type": "Full",
        "Start Time": "2020-08-21T00:30:10.000",
        "End Time": "2020-08-21T00:31:11.000",
        "Duration in Mins": 1,
        "Size of Application": 115343360,
        "Job Status": "Completed",
        "dt": "2020-08-21T00:00:00.000",
        "age": 0,
        "original_0": {
            "numberOfDays": "2",
            "serverName": "sgkcovldidbdev01"
        }
    }
]}

After deleting "original_0" JSON should be like below.

{
"entity": [{
        "JobId": 725454,
        "Operation Type": "Backup",
        "Client": "sgkcovldidbdev01",
        "Agent": "Oracle Database",
        "Instance": "INFSGDEV",
        "BackupSet": "default",
        "Subclient": "ArchiveLog",
        " Backup Type": "Full",
        "Start Time": "2020-08-21T18:30:19.000",
        "End Time": "2020-08-21T18:32:14.000",
        "Duration in Mins": 2,
        "Size of Application": 56623104,
        "Job Status": "Completed",
        "dt": "2020-08-21T00:00:00.000",
        "age": 0

    },
    {
        "JobId": 724699,
        "Operation Type": "Backup",
        "Client": "sgkcovldidbdev01",
        "Agent": "Oracle Database",
        "Instance": "INFSGDEV",
        "BackupSet": "default",
        "Subclient": "ArchiveLog",
        " Backup Type": "Full",
        "Start Time": "2020-08-21T12:30:09.000",
        "End Time": "2020-08-21T12:31:09.000",
        "Duration in Mins": 1,
        "Size of Application": 95420416,
        "Job Status": "Completed",
        "dt": "2020-08-21T00:00:00.000",
        "age": 0
    },
    {
        "JobId": 724056,
        "Operation Type": "Backup",
        "Client": "sgkcovldidbdev01",
        "Agent": "Oracle Database",
        "Instance": "INFSGDEV",
        "BackupSet": "default",
        "Subclient": "ArchiveLog",
        " Backup Type": "Full",
        "Start Time": "2020-08-21T06:30:11.000",
        "End Time": "2020-08-21T06:31:10.000",
        "Duration in Mins": 1,
        "Size of Application": 333447168,
        "Job Status": "Completed",
        "dt": "2020-08-21T00:00:00.000",
        "age": 0
    },
    {
        "JobId": 723035,
        "Operation Type": "Backup",
        "Client": "sgkcovldidbdev01",
        "Agent": "Oracle Database",
        "Instance": "INFSGDEV",
        "BackupSet": "default",
        "Subclient": "ArchiveLog",
        " Backup Type": "Full",
        "Start Time": "2020-08-21T00:30:10.000",
        "End Time": "2020-08-21T00:31:11.000",
        "Duration in Mins": 1,
        "Size of Application": 115343360,
        "Job Status": "Completed",
        "dt": "2020-08-21T00:00:00.000",
        "age": 0
    }
]}

I am using the below code to delete the property but it is not working.

var jsonRes = JsonConvert.DeserializeObject<dynamic>(json);

        foreach (var item in jsonRes)
        {
            item.Property("original_0").Remove();
           // Console.WriteLine(item.ToString());
        }

Thanks

2 Answers

You can use the following to remove all the properties from your json file:

            var json = File.ReadAllText(@"YOUR_PATH_HERE\json1.json");
            var jObj = JObject.Parse(json);

            foreach (JObject jObj in jObj["entity"].Children<JObject>())
            {
                JProperty originalJProperty = jObj.Property("original_0");
                originalJProperty?.Remove();
            }

According to the given Json string, the inside object original_0 can be removed simply in one go from whole entity array. It is done using Linq as:

var jsonRes = JObject.Parse(json);

jsonRes.Property("entity").Value
    .Select(jt => (JObject)jt)
    .ToList()
    .ForEach(obj =>
        obj
            .Properties()
            .ToList()
            .ForEach(v =>
            {
                if (v.Name == "original_0")
                    v.Remove();
            })
    );
Related