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

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.
Posted
Updated 7-Apr-18 0:24am
Comments
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?

I'm hoping you are using JSON.net (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.
   
Please check if this helps

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

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

           //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);
       }
   
Comments
Yaseer Arafat 19-Mar-17 15:10pm
   
You should Link my Solution you coppied entire my code :P
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.
   
v2
Comments
Wendelius 7-Apr-18 6:40am
   
Since you have a new question, it's best to post the question at Ask a question[^]

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