Click here to Skip to main content
15,936,565 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

I have developed an application in C# .Net that reads in JSON response from an API and writes it into a .json file.The JSON is very nested and complicated so for the scope of the project we figured out we will not convert it into Excel or CSV file and just write the data as it is.

The development is complete and now there is a need that those Json data need to be represented in a readable structure so I am being asked to populate them into Excel or CSV file by using some tool. I searched online but I could not find any good tool. I cannot post the json data online as it is sensitive corporate data so online converters are of no use to me. I am posting this question just to see if anyone knows any tool to change those data to csv or excel. I am worried that I might have to redo my project all over again and populate them into excel programmatically.

Thank you in advance for any idea you could provide.

What I have tried:

I tried looking online for different tools but could not decide for any.
Updated 7-Apr-18 0:24am
Abhijeet Bhurke 14-Feb-21 2:54am    
Hi begg, nice that you developed such a solution, can you share your prg, with me. i too am looking out for such a solution?

Hi As I tried with this code is working in localhost but in production getting error as we used Azure to publish our code
Can you please help me to overcome this.
Share this answer
Wendelius 7-Apr-18 6:40am    
Since you have a new question, it's best to post the question at Ask a question[^]
Please check if this helps

static void Main(string[] args)
           var json = @"{
                       ""employees"": [
                       { ""firstName"":""John"" , ""lastName"":""Doe"" },
                       { ""firstName"":""Anna"" , ""lastName"":""Smith"" },
                       { ""firstName"":""Peter"" , ""lastName"":""Jones"" }

       public static void jsonStringToCSV(string jsonContent)
           //used NewtonSoft json nuget package
           XmlNode xml = JsonConvert.DeserializeXmlNode("{records:{record:" + jsonContent + "}}");
           XmlDocument xmldoc = new XmlDocument();
           XmlReader xmlReader = new XmlNodeReader(xml);
           DataSet dataSet = new DataSet();
           var dataTable = dataSet.Tables[1];

           //Datatable to CSV
           var lines = new List<string>();
           string[] columnNames = dataTable.Columns.Cast<DataColumn>().
                                             Select(column => column.ColumnName).
           var header = string.Join(",", columnNames);
           var valueLines = dataTable.AsEnumerable()
                              .Select(row => string.Join(",", row.ItemArray));
           File.WriteAllLines(@"D:/Export.csv", lines);
Share this answer
Yaseer Arafat 19-Mar-17 15:10pm    
You should Link my Solution you coppied entire my code :P
I'm hoping you are using (newtonsoft) and already have classes created for your complicated JSON. If you aren't i would encourage you to do so. You can even take your json, paste it into json2csharp - generate c# classes from json[^] which will generate classes for your JSON.

One option is to research serializing C# objects to csv.

Something like this:

c# - Best practices for serializing objects to a custom string format for use in an output file - Stack Overflow[^]

What you are asking is possible. But without more knowledge of your project, this is the best that I can think of at this point.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900