0

I want to send data to KairosDB with post method but my Json is not correct. The Json must be like that:

{
    "name":"Hermle_1",
    "datapoints":[
        [
            "1530710258000",
            23.0
        ],
        [
            "1530710257000",
            21.0
        ]
    ],
    "tags":{
        "SensorName":"capteur_temperature"
    }
}

For datapoints I add Dictionary<string, float> to my object. What I have when I serialize it:

{
    "name":"Hermle_1",
    "datapoints":{
        "1530710258000":23.0,
        "1530710257000":21.0
    },
    "tags":{
        "SensorName":"capteur_temperature"
    }
}

If I change my dictionary .ToArray() I have this Json:

{
    "name":"Hermle_1",
    "datapoints":[
        {
            "Key":"1530710258000",
            "Value":23.0
        },
        {
            "Key":"1530710257000",
            "Value":21.0
        }
    ],
    "tags":{
        "SensorName":"capteur_temperature"
    }    
}

I don't know how to generate json in the expected format for the datapoints.

EDIT : How I create my object:

dynamic jsonObj = new ExpandoObject();
jsonObj.name = sd.Engine_name;

Dictionary<string, float> datapoints = new Dictionary<string, float>();
Dictionary<string, string> tags = new Dictionary<string, string>();

tags.Add("SensorName", sensor.Name);

//.........
while (reader.Read())
{    
    datapoints.Add(Convert.ToString(reader["time"]),
    Convert.ToSingle(reader["value"]));
}
//....
jsonObj.datapoints = datapoints;
jsonObj.tags = tags;

string a = JsonConvert.SerializeObject(jsonObj, Formatting.Indented);
croxy
  • 4,082
  • 9
  • 28
  • 46
MrB3NiT0
  • 137
  • 2
  • 16
  • Show us some code where you create the object. But as a first hint: `datapoints` should be an array of arrays, or list of lists if you prefer. Not a dictionary – derpirscher Jul 05 '18 at 08:35

2 Answers2

1

As I said in my comment above, datapoints is not a Dictionary but a List<List<object>> It must be object because you have to put string and float into the same list.

List<List<object>> datapoints = new List<List<object>>();
while (reader.Read()) {
    datapoints.Add(new List<object>{
        Convert.ToString(reader["time"]), 
        Convert.ToSingle(reader["value"])
    });
}
derpirscher
  • 14,418
  • 3
  • 18
  • 35
1

If you are not sure how to convert a json string into a class construct have a look at this helpful tool. The advantage of using a class structure in which you can fill your json data is, that you don't need to use any dynamic variables to serialize or deserialize your json object.

The generated C# code looks like this:

public class Tags
{
    public string SensorName { get; set; }
}

public class RootObject
{
    public string Name { get; set; }
    public List<List<object>> Datapoints { get; set; }
    public Tags Tags { get; set; }
}

After looking at the generated code you can see where you went wrong. The json for the datapoints uses a List() of Lists and not a Dictionary<string, float>.


Using this to generate your json data now is very simple:

var root = new RootObject();
root.Name = sd.Engine_name;
root.Tags = new List
{
    new Tags {SensorName = sensor.Name}
};

var datapoints = new List<List<object>>();
while (reader.Read()) 
{
    datapoints.Add(new List<object>{
        Convert.ToString(reader["time"]), 
        Convert.ToSingle(reader["value"])
    });
}

root.Datapoints = datapoints;

var json = JsonConvert.SerializeObject(root, Formatting.Indented);
croxy
  • 4,082
  • 9
  • 28
  • 46