locked
Convert Json object to csv. RRS feed

  • Question

  • User-2144897193 posted

    Hi Team,

    I need code to convert the below <gwmw class="ginger-module-highlighter-mistake-type-1" id="gwmw-15808013412615036232495">json</gwmw> object to <gwmw class="ginger-module-highlighter-mistake-type-1" id="gwmw-15808013412610635957519">csv</gwmw> file in c#.

    Tuesday, February 4, 2020 7:26 AM

All replies

  • User665608656 posted

    Hi kaarthikeyan,

    In fact, I did not see the json content you provided.

    If you want to convert the json content to a csv file, you can refer to the code:

    protected void Page_Load(object sender, EventArgs e)
            {
    
                var json = @"[
                           { ""firstName"":""John"" , ""lastName"":""Doe"" },
                           { ""firstName"":""Anna"" , ""lastName"":""Smith"" },
                           { ""firstName"":""Peter"" , ""lastName"":""Jones"" }
                           ]";
                jsonStringToCSV(json);
            }
            public static void jsonStringToCSV(string jsonContent)
            {
                var dataTable = (DataTable)JsonConvert.DeserializeObject(jsonContent, (typeof(DataTable)));
    
                //Datatable to CSV
                var lines = new List<string>();
                string[] columnNames = dataTable.Columns.Cast<DataColumn>().
                                                  Select(column => column.ColumnName).
                                                  ToArray();
                var header = string.Join(",", columnNames);
                lines.Add(header);
                var valueLines = dataTable.AsEnumerable()
                                   .Select(row => string.Join(",", row.ItemArray));
                lines.AddRange(valueLines);
                File.WriteAllLines(@"D:/Export.csv", lines);
            }

    Best Regards,

    YongQing.

    Wednesday, February 5, 2020 6:37 AM
  • User-2144897193 posted

    Thanks for the reply. I have <gwmw class="ginger-module-highlighter-mistake-type-3" id="gwmw-15815961285886124156973">complex json object</gwmw> which value <gwmw class="ginger-module-highlighter-mistake-type-3" id="gwmw-15815961285888292025381">pair</gwmw>.

    How to convert it into <gwmw class="ginger-module-highlighter-mistake-type-1" id="gwmw-15815961413339611252370">csv</gwmw> as well as <gwmw class="ginger-module-highlighter-mistake-type-1" id="gwmw-15815961460525530392685">txt</gwmw> file, please help on this.

    Thursday, February 13, 2020 12:17 PM
  • User665608656 posted

    Hi kaarthikeyan,

    The previous reply has been able to import the JSON content into the CSV file.

    How to convert it into csv as well as txt file, please help on this.

    In what form do you need to import the json format content into the txt file?

    Do you want to directly input the json content into the txt file, or convert it into the table format input or other forms?

    You can refer to this link:

    C# serialize and deserialize json to txt file

    Best Regards,

    YongQing.

    Friday, February 21, 2020 8:23 AM