Download blob storage and return Json object
Please reset the stream's position to 0
after reading the blob into the stream. So your code would be:
using (var stream = new MemoryStream()) { blob.DownloadToStream(stream); stream.Position = 0;//resetting stream's position to 0 var serializer = new JsonSerializer(); using (var sr = new StreamReader(stream)) { using (var jsonTextReader = new JsonTextReader(sr)) { var result = serializer.Deserialize(jsonTextReader); } } }
Both the question and accepted answer start by copying the entire stream into a MemoryStream
which is effectively a big byte array in memory. This step is unnecessary - it's more memory-efficient to stream the blob data directly to the object without buffering the bytes first:
using (var stream = await blob.OpenReadAsync())using (var sr = new StreamReader(stream))using (var jr = new JsonTextReader(sr)){ result = JsonSerializer.CreateDefault().Deserialize<T>(jr);}
In case you don't care for streaming and want a short and concise way:
var json = await blockBlob.DownloadTextAsync();var myObject = JsonConvert.DeserializeObject<MyObject>(json);