Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
CsvToJsonforCSAnomalyDetector.cs
static string CsvToJson(string csvPath, string granularity = "daily", bool hasHeaders = true)
{
char[] fieldSeparator = { ',' };
var lines = System.IO.File.ReadAllLines(csvPath);
// remove header
if (hasHeaders)
lines = lines.Skip(1).ToArray();
// build series
var arraySeries = new JArray();
foreach (var line in lines)
{
if (string.IsNullOrEmpty(line)) continue;
var fields = line.Split(fieldSeparator);
var jsonSerie = new JObject
{
["timestamp"] = fields[0],
["value"] = fields[1]
};
arraySeries.Add(jsonSerie);
}
var jobjectMain = new JObject
{
["granularity"] = granularity,
["series"] = arraySeries,
};
var jsonComplete = new JArray {jobjectMain};
return jsonComplete.ToString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.