3

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 2

5

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

It'll be more efficient to use streams than passing around chunks of byte[] or string

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.