Click here to Skip to main content
Click here to Skip to main content

Create CSV from JSON in C#

, 22 Mar 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
Using Extension Method and JSON Serializer


I faced a problem of creating CSV from a JSON object in ASP.NET. Here I am showing how to convert JSON to CSV with XML and DataSet.

Using the Code

I am using an Extension Method of DataTable to create CSV, XmlNodeReader to create XML from an XML node, JSON.DeserializeXmlNode. Let's start work:

var json={
"employees": [
{ "firstName":"John" , "lastName":"Doe" }, 
{ "firstName":"Anna" , "lastName":"Smith" }, 
{ "firstName":"Peter" , "lastName":"Jones" }

I need it deserialized first:

XmlNode xml = JSON.DeserializeXmlNode("{records:{record:" + json + "}}"); 
XmlDocument xmldoc = new XmlDocument();
//Create XmlDoc Object
//Create XML Steam 
var xmlReader = new XmlNodeReader(xmldoc);
DataSet dataSet = new DataSet();
//Load Dataset with Xml
//return single table inside of dataset
var csv = dataSet.Tables[0].ToCSV(",");

You have found that here is the extension method .ToCSV. Let's see how to create this.

public static string ToCSV(this DataTable table,string delimator)
    var result = new StringBuilder();
    for (int i = 0; i < table.Columns.Count; i++)
        result.Append(i == table.Columns.Count - 1 ? "\n" : delimator);
    foreach (DataRow row in table.Rows)
        for (int i = 0; i < table.Columns.Count; i++)
            result.Append(i == table.Columns.Count - 1 ? "\n" : delimator);
    return result.ToString().TrimEnd(new char[] { '\r', '\n' });
    //return result.ToString();

This is it. Thanks.


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


About the Author

Yaseer Arafat
Software Developer (Senior)
United States United States
• Experience in analysis, design and development of client/server, web based and n- tier application. Expert in developing windows applications, web applications, windows services and web services in Microsoft Visual Studio .Net IDE using C#.Net 1.1, C#.Net 2.0, VB.NET, ASP.NET, ADO.NET and SQL Server.
• Proficient in Web Services, SOAP, XML, XSL, XSLT, Xpath, XSL-FO, XPointer, DTD, XML Schema, CSS, DOM, HTML, AJAX and JavaScript, JQuery.
• Created deployment packages for Applications using Visual studio.Net Startup Project, which involves creating native image of an assembly, installing an assembly in Global assembly cache (GAC)
• Worked extensively with Data Adapter, Dataset, Data reader as a part of ADO.NET to access and Update database.
• Extensive experience in SQL Server Database design, Database maintenance, developing T-SQL queries, stored procedures, and triggers using SQL Server 2000/2005/2008.
• Good working knowledge with designer tools such as Microsoft Visio.
• Good working knowledge in designing Use Case, Class, Sequence, Collaboration, State,
Component, Deployment, Activity diagrams using UML
• Expertise in designing UML design patterns such as Adapter, Proxy, Bridge, and Facade and so on
• Expert in dealing with Private, Shared satellite and resource assemblies, configuring them with strong names, Deploying in GAC
• Expert in debugging an application using the debugging tools provided by the Visual Studio.Net IDE.
• Functional domain experience involves Financial, Telecom and Hospital management.
• Effective in working independently and collaboratively in teams.

Specialties:C#.NET,ASP.NET,ASP.NET MVC3,EnityFramework,JQuery,Linq to SQL,Oracle11g,TSQL,HTML,CSS,DevExpress,Crystal Reports.

Linkedin Profile
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

GeneralMy vote of 1 Pinmembercamilot196-Feb-15 15:11 
GeneralRe: My vote of 1 PinprofessionalYaseer Arafat9-Apr-15 10:08 
QuestionNearly! PinmemberBen Deed5-Aug-14 7:03 
AnswerRe: Nearly! PinprofessionalYaseer Arafat13-Aug-14 15:33 
QuestionNice article My vote of 5. But Have an issue PinmembercoolRahul_1219-May-14 20:14 
AnswerRe: Nice article My vote of 5. But Have an issue PinprofessionalYaseer Arafat30-Jun-14 15:00 
Questionerror for static class PinmemberAlfredo Lopez Morales20-Mar-14 14:11 
AnswerRe: error for static class PinmemberYaseer Arafat8-Apr-14 11:20 
AnswerRe: error for static class PinmemberYaseer Arafat17-Apr-14 9:55 
QuestionWant to save result at a specified location PinmemberShweta Kharade15-Jan-14 19:37 
AnswerRe: Want to save result at a specified location PinmemberYaseer Arafat17-Apr-14 9:57 
Questionupper PinmemberKasper Skov10-Dec-13 3:42 
QuestionNeed to escape commas and double quotation marks PinmemberThiện Mẫn Hoàng26-May-13 0:52 
AnswerRe: Need to escape commas and double quotation marks PinmemberYaseer Arafat16-Jan-14 17:46 
GeneralMy vote of 5 PinmemberCarsten V2.023-Mar-13 21:57 
GeneralRe: My vote of 5 PinmemberYaseer Arafat26-Mar-13 13:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150414.1 | Last Updated 22 Mar 2013
Article Copyright 2013 by Yaseer Arafat
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid