Search code examples
c#json.net-corejson.netchoetl

Csv stream to JSON conversion using ChoETL.Json failing


In my application I am getting csv stream and I am trying to convert Csv stream to JSON JToken dynamically, Here I will not be knowing the columns in the CSV so not able to define mapping. To convert this stream to JSON I am using ChoETL.Json CsvReader but this is failing to convert for some value since it is treating the string as DatTime type Here is the Sample function created to replicate the problem

public static void ConvertCsvStream()
        {
            JToken result = null;
            string delemeter = ",";
            string csvText = "\"Invoice Number\"\n\"1583-03\"\n\"1589-00\"";
            try
            {
                Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(csvText));
                using (StreamReader reader = new StreamReader(stream))
                {
                    using (var csv = new ChoCSVReader(reader).WithFirstLineHeader().WithDelimiter(delemeter))
                    {
                        result = JToken.FromObject(csv);
                    }
                }
            }
            catch(Exception ex)
            {

            }


        //return JToken.FromObject(sb.ToString());
    }

This is throwing error

Can't convert '1589-00' value from 'System.String' type to 'System.DateTime' type

For some values, it is able to convert but considering values as DateTime

{[{"Invoice Number": "1583-02-01T00:00:00"}, {"Invoice Number": "1583-03-01T00:00:00"}]}

Can anyone please help with this? I will not be able to create a Model for the csv since I will not be knowing the columns

Edit

after upgrading package to new version I was able create a method to convert csv stream to JToken

public static JToken ConvertCsvStream(Stream stream,string delemeter)
        {
            if (string.IsNullOrWhiteSpace(delemeter))
                delemeter = ",";

            using (StreamReader reader = new StreamReader(stream))
            {
                using (var csv=new ChoCSVReader(stream).QuoteAllFields().WithFirstLineHeader().WithDelimiter(delemeter))
                {
                    return JToken.FromObject(csv);
                }
            }
        }

Solution

  • It's a bug in the library, treating "1583-03" as datetime using auto type discovery. Please take the latest (1.2.0.1) nuget package for the fix.

    Here is the working sample

    string csvText = "\"Invoice Number\"\n\"1583-03\"\n\"1589-00\"";
    
    StringBuilder json = new StringBuilder();
    using (var r = ChoCSVReader.LoadText(csvText)
        .WithFirstLineHeader()
        .QuoteAllFields()
        )
    {
        using (var w = new ChoJSONWriter(json))
            w.Write(r);
    }
    Console.WriteLine(json.ToString());
    

    Output:

    [
     {
      "Invoice Number": "1583-03"
     },
     {
      "Invoice Number": "1589-00"
     }
    ]