Note: I'm following this guide: https://weblog.west-wind.com/posts/2012/aug/30/using-jsonnet-for-dynamic-json-parsing
My goal is to have a basic json structure which can adjust to dynamic structures inside it. For example, I have the following structures I am using to store trigger Json data:
public class TriggerJson
{
public string TriggerType { get; set; }
public string ConfiguredBy { get; set; }
public dynamic Trigger { get; set; }
}
The above structure will store any dynamic structure in Trigger property. For example, the one below:
public class ExpiryTriggerJson
{
public string ActionType { get; set; }
public TriggerRecipient[] Recipients { get; set; }
}
//... more Trigger structures like ExpiryTriggerJson above
I am able to store this fine in an array of TriggerJson. The issue is when I want to update the the structure. For example, I want to update or replace the Trigger property for one of the TriggerJson entries in the array. Below is my code that is having issues casting to Json type when assigning a new ExpiryTriggerJson object to TriggerJson.Trigger property. I am just casting the original TriggerJson array into JArray and going through each entry and when I find an appropriate entry to replace, I try to do so:
var expiryTriggerJson = new ExpiryTriggerJson
{
Recipients = taskRecipients.ToArray(),
ActionType = TriggerJsonHelper.ExpiryTriggerActionType_Task
};
JArray tjList = JArray.Parse(originalTriggersJson);
foreach (dynamic triggerJson in tjList)
{
if (triggerJson.TriggerType == ExpiryTriggerType)
{
triggerJson.Trigger = expiryTriggerJson; // this line is throwing an exception
triggerJson.ConfiguredBy = configuredBy;
}
}
return JsonConvert.SerializeObject(tjList);
My question is: is it possible to modify an existing json structure in this manner? Or should I have a different approach?
>();`