Click here to Skip to main content
15,037,168 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#



Convert Json to CSV
I am using json .net library
I have big json file and need to convert to .csv file I can able to deserialized array to datatable and

datatable to csv.
how to deserialized whole file to csv ? it has list,array some rootobjects

I have generated classes from json file using "json2csharp"

json file structure;
JavaScript
{
    "Active":"0",
    "Customer_Reference":"Suncor",
    "Datalink_Bus":"J1939",
    "Engine_Serial_Number":"66302847",
    "Equipment_ID":"0300",
    "FMI":"1",
    "Latitude":"56.727",
    "Longitude":"-111.381",
    "Message_Type":"FC",
    "Notification_Version":"2.004.002",
    "Occurrence_Count":"1",
    "Occurrence_Date_Time":"2016-10-11T14:34:38.150Z",
    "SPN":"100",
    "Sent_Date_Time":"2016-10-12T00:52:28.825Z",
    "Snapshots":
    [{ 
         "Parameter":
         [{
              "Name":"Truck speed",
              "Parameter_Source_Address":"",
              "Value":"0"
          },
          { 
              "Name":"HP Feedback - Alt In", 
              "Parameter_Source_Address":"", 
              "Value":"7"
          }],
          "Snapshot_DateTimestamp":"2016-10-11T14:34:09.000Z"
     }, 
     {
         "Parameter":
         [{
              "Name":"Truck speed",
              "Parameter_Source_Address":"",
              "Value":"0"
          },
          {
              "Name":"HP Feedback - Alt In",
              "Parameter_Source_Address":"",
              "Value":"7"
          }],
         "Snapshot_DateTimestamp":"2016-10-11T14:34:10.000Z"
     }]
}


Classes:
C#
 public class Parameter
    {
        public string Name { get; set; }

        public string Parameter_Source_Address { get; set; }

        public string Value { get; set; }

    }
 
public class Snapshot
    {
        public List<parameter> Parameter { get; set; }

        public string Snapshot_DateTimestamp { get; set; }

    }
public class RootObject
    {
        public string Active { get; set; }

        public string Customer_Reference { get; set; }

        public string Datalink_Bus { get; set; }

        public string Engine_Serial_Number { get; set; }

        public string Equipment_ID { get; set; }

        public List<snapshot> Snapshots { get; set; }
}


I am am getting only columns in datagrid view and empty rows.(null)

how do i parse multilayer Json TO CSV

What I have tried:

C#
public string ReadJsonFile(String Filepath)
{
    string json = string.Empty;
    // char seperator = ',';

    using (StreamReader read = new StreamReader(textBox1.Text))
    {
        json = read.ReadToEnd();

	var data1 = JsonConvert.DeserializeObject<parameter>(json);

        List<parameter> data = JsonConvert.DeserializeObject<list<parameter>>(json);

        DataTable dTable = ToDataTable(data)  ;//call function to convert list to datatable

        dataGridView1.DataSource = dTable;
    }
    return json;
}


With this I am getting 3 columns (Name,Parameter Address, Value)in datatable but they are empty ..no row data.
Posted
Updated 4-Nov-16 5:09am
v5
Comments
Philippe Mori 3-Nov-16 23:07pm
   
Uses code block for your code to make it readable... and augment the chance to get some help.

Also, what is the declaration of parameter class and where is the code to write to CSV file? We cannot tell you the problem in your code if there is no code!

1 solution

   
Comments
peoria123 4-Nov-16 14:25pm
   
Thank you !!

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