itisinteresting
itisinteresting

Reputation: 147

LINQ-to-Json or String-to-Json

I need to parse the json string at the bottom so that the sub-string representing the third level child

,"Media":{"ImageName":"Winter Twinkle"}  

is removed.

Original JSON:

[{
    "BusinessID"   : 1,
    "BusinessName" : "Artland Painters",
    "Category" : {
                    "ClassificationAlias" : "beauty-art",
                    "ClassificationName"  : "Beauty Art",
                    "Media"               : {
                                              "ImageName":"Winter Twinkle"
                                            }
                  }
}]

Thank you for your help.

Upvotes: 0

Views: 474

Answers (1)

Oleks
Oleks

Reputation: 32343

You could use e.g. Json.NET for this. First create an object from your json (I'm relying on the json snippet you provided):

var obj = (JArray)JsonConvert.DeserializeObject(json);

Then iterate through your object, find the Category property, and its last child:

var media = obj.Select(t => t["Category"].Last);

Then remove the nodes found:

foreach (var item in media)
    item.Remove();

And, finally, serialize the object into a string:

var repairedJson = JsonConvert.SerializeObject(obj);

This will give you the next result:

[{"BusinessID":1,"BusinessName":"Artland Painters","Category":{"ClassificationAlias":"beauty-art","ClassificationName":"Beauty Art"}}]

Upvotes: 2

Related Questions