Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Azure Function - CosmosDB Trigger and parsing document data

I am working on an Azure Function that runs on the CosmosDB trigger. Microsoft has created this example https://learn.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!

like image 762
p1ngu1n Avatar asked Feb 20 '26 14:02

p1ngu1n


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");
}
like image 190
Matias Quaranta Avatar answered Feb 23 '26 14:02

Matias Quaranta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!