0

I have a CSV which I am parsing to convert to JSON and then finally uploading to Azure blob.

This is an example of the CSV that I am reading and parsing. Each row will have its own JSON file.

humidity_sensor,    sensor1, {"temp":"22.3","batt":"3.11","ss":"28","humidity":"52.6","dp":"12.144704512672"}
humidity_sensor,    sensor1, {"batt":"3.14","ss":"16","humidity":"56.9","timestamp":1556568624,"temp":"21.7","dp":"12.784662018281"}
humidity_sensor,    sensor1, {"pressure":"5.14","prop2":"16","current":"56.9","temp":"21.7","dp":"12.784662018281"}

This is the model I want to serialize it to:

public class SensorModel
    {
        [JsonProperty("sensorId")]
        public string SensorId { get; set; }

        [JsonProperty("Inbound_data")]
        public Inbound Inbounddata { get; set; }
        [JsonProperty("ts")]
        public DateTime Ts { get; set; }
    }

    public class Inbound
    {
    }

So the output is of the following format:

   {
   "sensorId":"sensor1",
   "data_in":{

   },
   "ts":"2020-02-11T18:07:29Z"
}

The value in Inbound is the JSON from the CSV which is not constant and will change with each row of the CSV.

SensorModel sensorModel = new SensorModel 
                        {
                            SensorId = sensorId,
                            Ts = utcTimestamp,
                            Inbounddata  = new Inbound
                            {

                            }
                        };

But since I am not certain what is going to be in that node I can't define the properties in the Inbound class.

I tried using dynamic like this:

dynamic data = JObject.Parse(values[r, 4].ToString());

The right hand side of this expression is the value from CSV.

How can I dynamically figure out what properties are required under the inbound node. I could have simply updated the model to set the inbound property as JObject and then while creating the model assigned value to it but I need all the values of the inbound node to translate by looking up in the database.

Is there any way to achieve this?

4
  • JObject implements IDictionary and ICollection, you can iterate over it Commented Feb 24, 2020 at 15:14
  • Why not use object instead of you class Inbound? Commented Feb 24, 2020 at 15:14
  • @MAK you can also refer to this answer for some details Commented Feb 24, 2020 at 15:54
  • 1
    @PavelAnikhouski Thank you. The ICollection hint and using Dictionary<string,string> Inbounddata helped me with iterate over the JObject, retrieve properties and do the lookup. Commented Feb 24, 2020 at 16:23

1 Answer 1

1

You could declare Inbounddata as Dictionary<string,string>

public class SensorModel
{
    [JsonProperty("sensorId")]
    public string SensorId { get; set; }

    [JsonProperty("data_in")]
    public Dictionary<string,string> Inbounddata { get; set; }
    [JsonProperty("ts")]
    public DateTime Ts { get; set; }
}

For example,

var sensorModel = new SensorModel
{
    SensorId = "2",
    Ts = DateTime.Now,
    Inbounddata = new Dictionary<string,string>
    {
        ["temp"] = "22.5",
        ["batt"] = "3.11",
        ["ss"] = "22"
    }

};

var result = JsonConvert.SerializeObject(sensorModel);

Output

{"sensorId":"2","data_in":{"temp":"22.5","batt":"3.11","ss":"22"},"ts":"2020-02-24T20:46:39.9728582+05:30"}
Sign up to request clarification or add additional context in comments.

2 Comments

Even if I declare it so, I need to traverse all the properties, lookup values in DB and then translate them and then assign to the inbound node. Your solution is adding values to the Inbounddata node statically or am I missing something. I read the comment by Pavel and now am trying to iterate over the JObject. This way I am reading the properties and will translate them. I am still working on it.
Thanks for this though. I used public Dictionary<string,string> Inbounddata { get; set; } and it helped me assign the values.

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.