Search code examples
c#choetl

Header JSON Converting CSV to JSON


Hi im tring to convert CSV String to JSON, but the Header of the JSON has some issues with Encode i think.

Here the code and the Output:

[{... "Endere_o_4": "", "Endere_o_5": "", "Endere_o_6": "", "C_digo_Postal": "1000-045", ...]}


Expected Result: [{... "Endereço_4": "", "Endereço_5": "", "Endereço_6": "", "Código_Postal": "1000-045", ...]}


public void MssCSVtoJSON(string ssCSV, out string ssJSON)
        {
            ssJSON = "";

            ChoCSVFileHeaderConfiguration headerConfiguration = new ChoCSVFileHeaderConfiguration(null, new System.Globalization.CultureInfo("pt-PT"));
            ChoCSVRecordConfiguration config = new ChoCSVRecordConfiguration();
            config.FileHeaderConfiguration = headerConfiguration;

            StringBuilder sb = new StringBuilder();
            using (var p = ChoCSVReader.LoadText(ssCSV,Encoding.Unicode, config, null).WithFirstLineHeader())  {
                using (var w = new ChoJSONWriter(sb))   {
                    w.Configuration.Encoding = Encoding.Unicode;
                    w.Write(p);
                }
            }
            ssJSON = sb.ToString();
            // TODO: Write implementation for action
        } // MssCSVtoJSON

Solution

  • It is known issue, put a fix and pushed ChoETL 1.1.0.5-alpha2 nuget package.

    Here is working sample

    string csv = @"Endereço_4, Endereço_5
    1, 11
    2, 22";
    StringBuilder output = new StringBuilder();
    using (var r = ChoCSVReader.LoadText(csv).WithFirstLineHeader())
    {
        using (var w = new ChoJSONWriter(output))
            w.Write(r);
    }
    
    Console.WriteLine(output);
    

    Output:

    [
     {
      "Endereço_4": "1",
      "Endereço_5": "11"
     },
     {
      "Endereço_4": "2",
      "Endereço_5": "22"
     }
    ]