using Azure Function with Blob Trigger. How do I read the JSON file. In the sample it reads only the length. Should I use Stream or CloudBlockBlob ? I need to deseralize json using c#.
2 Answers
Something like the below will do the trick.
[FunctionName("BlobTriggerCSharp")]
public static void Run([BlobTrigger("demo/{name}")] Stream stream, string name, TraceWriter log)
{
var serializer = new JsonSerializer();
using (var sr = new StreamReader(stream))
using (var jsonTextReader = new JsonTextReader(sr))
{
var person = (Person)serializer.Deserialize(jsonTextReader, typeof(Person));
// Do something with person.
}
}
See Json.Net docs for more details - https://www.newtonsoft.com/json/help/html/DeserializeWithJsonSerializerFromFile.htm
Comments
You can bind as a string, and then the SDK will read the contents for you. Then you can convert to JSON.
[FunctionName("BlobTriggerCSharp")]
public static void Run([BlobTrigger("demo/{name}")] string contents)
{
var person = JsonConvert.DeserializeObject<Person>(contents);
}
You could also bind to byte[] to get the byte contents.
1 Comment
Kevin Smith
It'll be more efficient to use streams than passing around chunks of
byte[] or string