0

I am importing .CSV file from an angular app into MVC and i am able to get the files like this

Int32 strLen, strRead;
            System.IO.Stream stream = Request.InputStream;
            strLen = Convert.ToInt32(stream.Length);
            byte[] strArr = new byte[strLen];
            strRead = stream.Read(strArr, 0, strLen);

here the files which is being imported is converted into byte[] because i am reading the file using

System.IO.Stream stream = Request.InputStream

Then i convert it into string like this

 string a = System.Text.Encoding.UTF8.GetString(strArr);

and try to split the content and retrieve the data but it becomes very complex, i wonder if there is any alternate way for it. In a simple .CSV file like this

enter image description here I get the result after converting the byte[] to string like this

enter image description here

and once i apply logic for splitting the string and retrieving the data, the logic gets very messy like this

enter image description here

Is there any efficinet way where i can convert the imported .CSV file to JSON

Lijin Durairaj
  • 3,341
  • 11
  • 31
  • 50
  • 1
    You need to parse the CSV, you can use [CSVHelper](https://joshclose.github.io/CsvHelper/) then you just use a JSON serializer – johnny 5 Aug 31 '17 at 14:06
  • i am not reading the file from a file location, i am exporting the .CSV file from HTML page using the input type='file' button, i am getting the file as a stream – Lijin Durairaj Aug 31 '17 at 14:13
  • 1
    stream is just a file in memory, CSVHelper has no problem reading that – johnny 5 Aug 31 '17 at 14:15
  • i dont find any overload for the stream in CSVHelper, i get the overload for System.IO.TextReader, can you help – Lijin Durairaj Aug 31 '17 at 14:18

2 Answers2

1

Save stream as text file in to the TEMP folder.

Use any parcer for working with CSV file. (Example FileHelpers)

Use any Json helper to convert it to the output format. (Example: newtonsoft)

kkost
  • 3,640
  • 5
  • 41
  • 72
  • i dont want to save the file in any folder because i have rights issue to access any file in the client environment – Lijin Durairaj Aug 31 '17 at 14:28
  • @LijinDurairaj Did you try get acces to System.IO.Path.GetTempPath() ? At least you can get access to your IIS working folder where your build placing – kkost Aug 31 '17 at 14:31
  • actually i dont want to place the file in a folder at all – Lijin Durairaj Aug 31 '17 at 14:45
  • @LijinDurairaj in any case before your parcing will start, program will read all stream from begin to end, so there is no performance profit in case with stream using without file, but up to you :) – kkost Aug 31 '17 at 15:14
1

You can use Cinchoo ETL - an open source library, to convert CSV to JSON easily.

using (var parser = new ChoCSVReader("IgnoreLineFile1.csv")
    .WithField("PolicyNumber", 1)
    .WithField("VinNumber", 2)
    .Configure(c => c.IgnoreEmptyLine = true)
    .Configure(c => c.ColumnCountStrict = true)
    )
{
    using (var writer = new ChoJSONWriter("ignoreLineFile1.json")
            .WithField("PolicyNumber", fieldName: "Policy Number")
            .WithField("VinNumber", fieldName: "Vin Number")
        )
        writer.Write(parser.Skip(1));
}

In above, you can pass stream to the reader and writer as well for your requirement.

Hope this will help.

Disclaimer: I'm the author of this library.

Cinchoo
  • 6,088
  • 2
  • 19
  • 34