Search code examples
asp.net-mvcjsonprojectionjavascriptserializer

remove children from object for JSON serialisation


I have an object with children that I'm using JavaScriptSerializer to convert to JSON. I'm using MVC and using the JavaScriptSerializer in a view as so:

@Html.Raw(serializer.Serialize(Model.Designs)

My "designs" object has the children "DesignDeliveries" but I need to remove these from the JSON string so I've tried using projection but cannot get the format I require

So my Designs object is as follows:

[{
"BookingDesignId": "e90e9500-0a6a-4d1b-a82a-fa7ca2d7c034",
"DesignName": "Design 600",
"Quantity": 100,
"DesignDeliveries": [{
    "BookingId": "3706a896-3b8f-454f-acc8-6540441a3e4a",
    "Quantity": 50,
    "BookingDesignId": "e90e9500-0a6a-4d1b-a82a-fa7ca2d7c034",
    "DepotId": "9"
},
{
    "BookingId": "3706a896-3b8f-454f-acc8-6540441a3e4a",
    "Quantity": 50,
    "BookingDesignId": "e90e9500-0a6a-4d1b-a82a-fa7ca2d7c034",
    "DepotId": "18"
}]
}];

However, I need:

[{
"BookingDesignId": "e90e9500-0a6a-4d1b-a82a-fa7ca2d7c034",
"DesignName": "Design 600",
"Quantity": 100,
"DesignDeliveries": [];

Does anyone know how to achieve this?

Thanks in advance


Solution

  • Clone Model.Designs, clear your collection of DesignDeliveries, serialize cloned Designs again.