I am using the following code to convert csv to json via C#:
var jsonWrtr = new StreamWriter(targetFile + ".json");
var csv = new List<string[]>(); // or, List<YourClass>
var lines = System.IO.File.ReadAllLines(targetFile + ".csv");
foreach (string line in lines) { csv.Add(line.Split(',')); }
string json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(csv);
jsonWrtr.Write(json);
jsonWrtr.Close();
Here is an example of what it outputs:
[["Type","Id","Name","Age","Org","DOB","FirstName","LastName","Flight"],["1","2","3","4","5","6","7","8","9"],["1","2","3","4","5","6","7","8","9"]]
I would like the output in the following format:
[{
"Type" : "1",
"Id" : "2",
"Name" : "3",
"Age" : "4",
"Org" : "5",
"Dob" : "6", ,
"FirstName" : "7",
"LastName" : "8",
"Flight" : "9",
}, {
"Type" : "1",
"Id" : "2",
"Name" : "3",
"Age" : "4",
"Org" : "5",
"Dob" : "6", ,
"FirstName" : "7",
"LastName" : "8",
"Flight" : "9",
}
]
Any ideas why it's not working ?
asked May 6, 2016 at 16:12
thatOneGuy
10.7k9 gold badges59 silver badges93 bronze badges
-
3That json is not invalid, just not the format you expectedTamas Hegedus– Tamas Hegedus2016年05月06日 16:15:05 +00:00Commented May 6, 2016 at 16:15
-
3And this is exactly what you fed into the serializer. You just loaded the csv into a string matrix, and serialized it directly, never made objects out of itTamas Hegedus– Tamas Hegedus2016年05月06日 16:15:57 +00:00Commented May 6, 2016 at 16:15
-
I'm voting to close this question as off-topic because the things are behaving exactly as they should be.Jeremy J Starcher– Jeremy J Starcher2016年05月06日 16:20:12 +00:00Commented May 6, 2016 at 16:20
-
The .csv file's structure is not the format that you expected. Please show a part of your file data.amirpaia– amirpaia2016年05月06日 16:24:23 +00:00Commented May 6, 2016 at 16:24
1 Answer 1
The output the code produces is the expected output. You have to convert the data to the format you want. Here is an example how to do it:
static void Main(string[] args)
{
var lines = @"A,B,C
1,2,3
4,5,6".Replace("\r", "").Split('\n');
var csv = lines.Select(l => l.Split(',')).ToList();
var headers = csv[0];
var dicts = csv.Skip(1).Select(row => Enumerable.Zip(headers, row, Tuple.Create).ToDictionary(p => p.Item1, p => p.Item2)).ToArray();
string json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(dicts);
Console.WriteLine(json);
}
answered May 6, 2016 at 16:32
Tamas Hegedus
30.1k12 gold badges67 silver badges102 bronze badges
Sign up to request clarification or add additional context in comments.
3 Comments
thatOneGuy
Sorry I was AFK for the weekend and couldn't get back until now. Apologies for the question I was obviously being lazy and not reading it properly. But this works perfectly and has helped me a lot. I am reading from file so instead of your ""var lines = @"A,B,C ...."" I have : ""var linesCSV = System.IO.File.ReadAllLines(targetFile);"" where targetFile is the name of my csv. Then I use linesCSV instead of lines. If you could explain what the dicts line is doing ? Some of it's self explanatory but some of it isn't. Thanks again
Tamas Hegedus
@thatOneGuy It basically just creates an array of dictionaries from the lines of the csv, with keys being the values in the first row, and values being the values from the current row at the same column as the key. And then the json serializer serializes the dictionaries as json objects by default.
James
namespace for System.Web.Script is: System.Web.Extension
lang-cs