I'm creating a database seeder for Cosmos DB (a document or JSON based DB). Some of the C# models have a property Config that is JSON so I've been using this type of code to set that property:
Config = JObject.FromObject(new { })
which works as does actually setting property(ies) inside the object:
Config = JObject.FromObject(new
{
contextOptionSource = "$.domains.governmentEntityType_active"
}),
However, I can't figure out how to set Config to an array of objects. I tried actually using C# Models thinking that JObject would convert them for me like so:
Config = JObject.FromObject(
new List<Question>
{
new Question
{
Key = "contact",
Label = "Contact Person",
HelpText = "",
Config = JObject.FromObject(new {}),
Type = "text",
ContextTarget = "$.data.contact"
},
new Question
{
Key = "company",
Label = "Company Name",
HelpText = "",
Config = JObject.FromObject(new {}),
Type = "text",
ContextTarget = "$.data.company"
}
}),
This compiled OK but when I run I get a runtime error "Object serialized to Array. JObject instance expected.'" Am I wrong to think that JObject should convert the C# models to JSON? If they have to be generic objects that's fine but I can't get the syntax right that the FromObject method will accept multiple objects inside this Config property.
Edit: Here's the JSON I'm trying to produce:
"config": {
"questions": [{
"key": "contact",
"label": "Contact Person",
"helpText": "Contact Person",
"config": {},
"type": QuestionKind.Textbox,
"contextTarget": "$.data.contact",
"enabledRule": null,
"validationRules": [],
"visibleRule": null
},
{
"key": "company",
"label": "Company Name",
"helpText": "Company Name",
"config": {},
"type": QuestionKind.Textbox,
"contextTarget": "$.data.company",
"enabledRule": null,
"validationRules": [],
"visibleRule": null
}]
}