ronconsoda
ronconsoda

Reputation: 117

Parsing Json in c# in Azure functions

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?

Upvotes: 0

Views: 3679

Answers (3)

4c74356b41
4c74356b41

Reputation: 72191

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.

Upvotes: 0

Panagiotis Kanavos
Panagiotis Kanavos

Reputation: 131722

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;

Upvotes: 3

D-Shih
D-Shih

Reputation: 46249

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.
}

Upvotes: 2

Related Questions