0

In my case I receive a Json with multiples lines, in Azure with this format. The number of lines that I receive are variable. The number of variables that I receive are also variable ( I can have up to 20)

{"Data":[
{"name":"Variable A","value":0.321721,"timecreated":"2018-1-15T11:10:7.977Z"},
{"name":"Variable B","value":-8.932533,"timecreated":"2018-1-15T11:10:8.17Z"},
{"name":"Variable C","value":-7.068326,"timecreated":"2018-1-15T11:10:8.58Z"},
{"name":"Variable A","value":-3.580420,"timecreated":"2018-1-15T11:10:8.98Z"},
{"name":"Variable C","value":1.549976,"timecreated":"2018-1-15T11:10:7.977Z"},
{"name":"Variable A","value":-8.701625,"timecreated":"2018-1-15T11:10:8.17Z"}]}

I would like to use something similar to this:

#r "Newtonsoft.Json"
using Newtonsoft.Json;

public class Variables
{
public string name="";
public string value="";
public string timecreated="";

}


public static void Run(Stream myBlob, string name, TraceWriter log)
{
    log.Info($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes");
    var serializer = new JsonSerializer();

    using (var sr = new StreamReader(myBlob))
    using (var jsonTextReader = new JsonTextReader(sr))
    {
        var Values= (Variables)serializer.Deserialize(jsonTextReader,typeof(Variables));

        // Do something with Values.
    }
}

Having Values as an array of variables that aftewards I can handle for DDBB. Also a multiple dictionary list would be a good solution....Any idea?

3 Answers 3

3

Your JSON string doesn't contain an array of Variables, it contains one object whose Data property is an array of Variables.

To deserialize it create an object with a Data property :

public class Variable
{
    public string name{get;set;}
    public string value {get;set;}
    public string timecreated {get;set;}    
}

public class MyDTO
{
    public Variable[] Data{get;set;}
}

Deserializing the string is a single call:

var dto=JsonConvert.DeserializeObject<MyDTO>(json);
var values=dto.Data;
Sign up to request clarification or add additional context in comments.

3 Comments

I am using this, using (var sr = new StreamReader(myBlob)) string sss = sr.ReadToEnd(); var dto = JsonConvert.DeserializeObject<MyDTO>(sss);, ann dto.Data is null.
The code works. Just try it with the string you posted in the question. What's the string you load from the file? If it's empty, or the schema is different, Data will be empty.
you are right, I have /r in my Json. I think that is the reason, i have hardcoded it and it works.you are totally right
2

You can use JsonConvert.DeserializeObject to Convert a modelList

public class Datum
{
    public string name { get; set; }
    public double value { get; set; }
    public string timecreated { get; set; }
}

public class RootObject
{
    public List<Datum> Data { get; set; }
}

public static void Run(Stream myBlob, string name, TraceWriter log)
{

    Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject> reciveList =
       new Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>();
    List<Datum> list  = reciveList.Data 
    //Do something with list.
}

Comments

0

I was using a dynamic object, which in my mind looks better (you dont have to créate class for it and it can change and nothing will break, well to some extent).

dynamic parsed = JsonConvert.DeserializeObject(data);

and after that you can just Access json properties like object properties:

parsed.name

I'm not sure how that would work with streamreader though.

Comments

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.