I have JSON
string as below
[{
"attachments": [{ "comment": "communication", "comment_date_time": "2035826"} ],
"spent_hours": "4.00",
"description": ""
},
{
"attachments": [],
"spent_hours": "4.00",
"description": ""
}]
How can i remove the attachments
attribute from the JSON
string using C#. I am using JSON.net.
To remove JSON element, use the delete keyword in JavaScript.
JsonObject::remove() removes a key-value pair from the object pointed by the JsonObject . If the JsonObject is null, this function does nothing.
Using Linq
var jArr = JArray.Parse(json);
jArr.Descendants().OfType<JProperty>()
.Where(p => p.Name == "attachments")
.ToList()
.ForEach(att=>att.Remove());
var newJson = jArr.ToString();
or using anonymous classes
var anon = new[] { new{spent_hours="", description=""} };
var newJson = JsonConvert.SerializeObject(
JsonConvert.DeserializeAnonymousType(json, anon));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With