How to remove specific attribute from JSON string using C #

I have a JSON string below

 [{ "attachments": [{ "comment": "communication", "comment_date_time": "2035826"} ], "spent_hours": "4.00", "description": "" }, { "attachments": [], "spent_hours": "4.00", "description": "" }] 

How to remove attachments attribute from JSON string using C #. I am using JSON.net.

+7
source share
1 answer

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)); 
+18
source

All Articles