none
Not able to convert JSON To CSV RRS feed

  • Question

  • I am trying to parse the Json content from web API and convert to csv using the following code but isnt working. the error that i get is " Cannot add a nested relation or an element column to a table containing a SimpleContent column."

    Thanks in advance for any help!!!

    using System;
    using System.IO;
    using System.Net;
    using System.Text;
    using System.Collections.Generic;
    using System.Data;
    using System.Linq;
    using System.Data.DataSetExtensions;
    using System.Web;
    using Newtonsoft.Json;
    using CsvHelper;
    using System.Net.Http;
    using System.Runtime;
    using System.Xml;




    namespace SerraviewAPI
    {
        public class Webrequest
        {
            static string content;
            public static void Main()
            {

    Webrequest.content ="{\"id\":\"Table_4155\",\"headers\":[\"OrgUnitName\",\"CostCentre\",\"Level\",\"OrgUnitLevel0\",\"CostCentreOnLevel0\",\"OrgUnitLevel1\",\"CostCentreOnLevel1\",\"OrgUnitLevel2\",\"CostCentreOnLevel2\",\"OrgUnitLevel3\",\"CostCentreOnLevel3\",\"OrgUnitLevel4\",\"CostCentreOnLevel4\",\"OrgUnitLevel5\",\"CostCentreOnLevel5\",\"OrgUnitLevel6\",\"CostCentreOnLevel6\"],\"rows\":[[\"Accounting-Europe\",\"5583\",\"6\",\"LinkedIn\",\"D000\",\"GWS/FIN\",\"D050\",\"CorporateManagement\",\"D1P0\",\"Finance&Accounting\",\"D2P2\",\"CAOOrganization\",\"D3P1\",\"ControllershipOrg\",\"D4P1\",\"Accounting-Europe\",\"5583\"],[\"AdvertisingOperations\",\"1900\",\"6\",\"LinkedIn\",\"D000\",\"GSO\",\"D020\",\"Advertising\",\"D1C0\",\"AdvertisingOperations\",\"D2C1\",\"AdvertisingOperations\",\"D3C1\",\"AdvertisingOperations\",\"D4C1\",\"AdvertisingOperations\",\"1900\"]] }";       
    jsonStringToCSV(content);
           }

           public static void jsonStringToCSV(string jsonContent)
           {
               //used NewtonSoft json nuget package
               XmlNode xml = JsonConvert.DeserializeXmlNode("{records:{record:" + Webrequest.content + "}}");
               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(@"C:\Users\Feed\1.csv", lines);
           }
    }
    }

    Friday, January 5, 2018 12:13 AM

All replies

  • Hello,

    First off, no answer here just confirmation that a DataSet can not handle your data relationships.

    You can't use a DataSet, it does not know how to handle your relationships. You are good here

    var contents = "{records:{record:" + "{\"id\":\"Table_4155\",\"headers\":[\"OrgUnitName\",\"CostCentre\",\"Level\",\"OrgUnitLevel0\",\"CostCentreOnLevel0\",\"OrgUnitLevel1\",\"CostCentreOnLevel1\",\"OrgUnitLevel2\",\"CostCentreOnLevel2\",\"OrgUnitLevel3\",\"CostCentreOnLevel3\",\"OrgUnitLevel4\",\"CostCentreOnLevel4\",\"OrgUnitLevel5\",\"CostCentreOnLevel5\",\"OrgUnitLevel6\",\"CostCentreOnLevel6\"],\"rows\":[[\"Accounting-Europe\",\"5583\",\"6\",\"LinkedIn\",\"D000\",\"GWS/FIN\",\"D050\",\"CorporateManagement\",\"D1P0\",\"Finance&Accounting\",\"D2P2\",\"CAOOrganization\",\"D3P1\",\"ControllershipOrg\",\"D4P1\",\"Accounting-Europe\",\"5583\"],[\"AdvertisingOperations\",\"1900\",\"6\",\"LinkedIn\",\"D000\",\"GSO\",\"D020\",\"Advertising\",\"D1C0\",\"AdvertisingOperations\",\"D2C1\",\"AdvertisingOperations\",\"D3C1\",\"AdvertisingOperations\",\"D4C1\",\"AdvertisingOperations\",\"1900\"]] }" + "}}";
    XmlNode xml = JsonConvert.DeserializeXmlNode(contents);
    XmlDocument xmldoc = new XmlDocument();
    
    xmldoc.LoadXml(xml.InnerXml);
    var xmlReader = new XmlNodeReader(xmldoc);
    So I'm guessing the dataSet.ReadXml is the issue.  So you will need to figure out a method w/o using a DataSet.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, January 5, 2018 1:16 AM
    Moderator