I am converting a csv file into a json file with c# but the file I receive is just a bunch of values. I want to save them into a key => value format when converting to json.
My code:
static void ReadFromCsv()
{
var csv = new List<string[]>();
var lines = System.IO.File.ReadAllLines(@"C:\path\details.csv");
foreach (string line in lines)
csv.Add(line.Split(','));
var json = JsonSerializer.Serialize(csv);
File.WriteAllText(@"C:\path2\jsontest.json", json);
}
The json file ends up like this:
[
"20-7-2020",
"Ben",
"Smith",
"+27745004488",
"Orange",
"Tree",
"54 Victoria Road"
],
[
"20-7-2020",
"Jade",
"Hodsin",
"+27743456060",
"Apple",
"Plant",
"20 Small Street"
]
My Goal is to make the json look like the following for easy storage into a sql DB:
[
"Date" : "20-7-2020",
"Name" : "Ben",
"Surname" : "Smith",
"Cell" : "+27745004488",
"Food" : "Orange",
"Source" : "Tree",
"Address" : "54 Victoria Road"
],
[
"Date" : "20-7-2020",
"Name" : "Jade",
"Surname" : "Hodsin",
"Cell" : "+27743456060",
"Food" : "Apple",
"Source" : "Plant",
"Address" : "20 Small Street"
]