C# Azure Function - CosmosDB Trigger and parsing document data

Viewed 1840

I am working on an Azure Function that runs on the CosmosDB trigger. Microsoft has created this example https://docs.microsoft.com/en-us/azure/azure-functions/functions-create-cosmos-db-triggered-function which works out nicely concerning the trigger.

I am now asking myself how to parse the returned document in a smooth way. The data I get back from the trigger function is from the class Microsoft.Azure.Documents.Document (IReadOnlyList<Document> documents).

Any nice idea on how to parse those data into Json objects or comparable?

Thx!

1 Answers

Document can be deserialized to any type you want, for example:

foreach (Document document in documents)
{
    MyClass myClass = JsonConvert.DeserializeObject<MyClass>(document.ToString());
}

You can also read any of its properties:

foreach (Document document in documents)
{
    string myPropertyValue = document.GetPropertyValue<string>("myProperty");
}
Related