From 2c71cff268956e9a3ddc3b37f514b32a571c09d4 Mon Sep 17 00:00:00 2001 From: KarthikaSF4773 Date: Fri, 30 May 2025 11:01:47 +0530 Subject: [PATCH] 41924-JSONtoCSVExample --- .../.NET/JSON to CSV/JSON to CSV.sln | 25 +++++++++++ .../JSON to CSV/JSON to CSV/Data/Input.json | 5 +++ .../JSON to CSV/JSON to CSV.csproj | 22 ++++++++++ .../JSON to CSV/JSON to CSV/Output/.gitkeep | 0 .../.NET/JSON to CSV/JSON to CSV/Program.cs | 35 +++++++++++++++ FAQ/Conversions/.NET/JSON to CSV/README.md | 44 +++++++++++++++++++ 6 files changed, 131 insertions(+) create mode 100644 FAQ/Conversions/.NET/JSON to CSV/JSON to CSV.sln create mode 100644 FAQ/Conversions/.NET/JSON to CSV/JSON to CSV/Data/Input.json create mode 100644 FAQ/Conversions/.NET/JSON to CSV/JSON to CSV/JSON to CSV.csproj create mode 100644 FAQ/Conversions/.NET/JSON to CSV/JSON to CSV/Output/.gitkeep create mode 100644 FAQ/Conversions/.NET/JSON to CSV/JSON to CSV/Program.cs create mode 100644 FAQ/Conversions/.NET/JSON to CSV/README.md diff --git a/FAQ/Conversions/.NET/JSON to CSV/JSON to CSV.sln b/FAQ/Conversions/.NET/JSON to CSV/JSON to CSV.sln new file mode 100644 index 00000000..6e252710 --- /dev/null +++ b/FAQ/Conversions/.NET/JSON to CSV/JSON to CSV.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.14.36127.28 d17.14 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JSON to CSV", "JSON to CSV\JSON to CSV.csproj", "{30711D59-1CD1-4D4F-A418-10F6E91247C6}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {30711D59-1CD1-4D4F-A418-10F6E91247C6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {30711D59-1CD1-4D4F-A418-10F6E91247C6}.Debug|Any CPU.Build.0 = Debug|Any CPU + {30711D59-1CD1-4D4F-A418-10F6E91247C6}.Release|Any CPU.ActiveCfg = Release|Any CPU + {30711D59-1CD1-4D4F-A418-10F6E91247C6}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {E22A3F55-7C52-46AD-8EA4-5F5BEB67552B} + EndGlobalSection +EndGlobal diff --git a/FAQ/Conversions/.NET/JSON to CSV/JSON to CSV/Data/Input.json b/FAQ/Conversions/.NET/JSON to CSV/JSON to CSV/Data/Input.json new file mode 100644 index 00000000..57f39600 --- /dev/null +++ b/FAQ/Conversions/.NET/JSON to CSV/JSON to CSV/Data/Input.json @@ -0,0 +1,5 @@ +[ + { "Id": 1, "Name": "Apple", "Price": 0.99 }, + { "Id": 2, "Name": "Banana", "Price": 0.59 }, + { "Id": 3, "Name": "Cherry", "Price": 2.99 } +] diff --git a/FAQ/Conversions/.NET/JSON to CSV/JSON to CSV/JSON to CSV.csproj b/FAQ/Conversions/.NET/JSON to CSV/JSON to CSV/JSON to CSV.csproj new file mode 100644 index 00000000..b701744f --- /dev/null +++ b/FAQ/Conversions/.NET/JSON to CSV/JSON to CSV/JSON to CSV.csproj @@ -0,0 +1,22 @@ + + + + Exe + net8.0 + JSON_to_CSV + enable + enable + + + + + + + + + + Always + + + + diff --git a/FAQ/Conversions/.NET/JSON to CSV/JSON to CSV/Output/.gitkeep b/FAQ/Conversions/.NET/JSON to CSV/JSON to CSV/Output/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/FAQ/Conversions/.NET/JSON to CSV/JSON to CSV/Program.cs b/FAQ/Conversions/.NET/JSON to CSV/JSON to CSV/Program.cs new file mode 100644 index 00000000..0f361ae3 --- /dev/null +++ b/FAQ/Conversions/.NET/JSON to CSV/JSON to CSV/Program.cs @@ -0,0 +1,35 @@ +using System.Data; +using Newtonsoft.Json; +using Syncfusion.XlsIO; + +class Program +{ + static void Main() + { + //Load JSON file + string jsonPath = Path.GetFullPath("Data/Input.json"); + string jsonData = File.ReadAllText(jsonPath); + + //Deserialize to DataTable + DataTable dataTable = JsonConvert.DeserializeObject(jsonData); + + //Initialize ExcelEngine + using (ExcelEngine excelEngine = new ExcelEngine()) + { + IApplication application = excelEngine.Excel; + application.DefaultVersion = ExcelVersion.Xlsx; + IWorkbook workbook = application.Workbooks.Create(1); + IWorksheet sheet = workbook.Worksheets[0]; + + //Import DataTable to worksheet + sheet.ImportDataTable(dataTable, true, 1, 1); + + //Saving the workbook as CSV + FileStream outputStream = new FileStream(Path.GetFullPath("Output/Sample.csv"), FileMode.Create, FileAccess.ReadWrite); + workbook.SaveAs(outputStream, ","); + + //Dispose streams + outputStream.Dispose(); + } + } +} diff --git a/FAQ/Conversions/.NET/JSON to CSV/README.md b/FAQ/Conversions/.NET/JSON to CSV/README.md new file mode 100644 index 00000000..04b7ce74 --- /dev/null +++ b/FAQ/Conversions/.NET/JSON to CSV/README.md @@ -0,0 +1,44 @@ +# How to convert JSON document to CSV format document? + +Step 1: Create a New C# Console Application Project. + +Step 2: Name the Project. + +Step 3: Install the [Syncfusion.XlsIO.Net.Core](https://www.nuget.org/packages/Syncfusion.XlsIO.Net.Core) and [Newtonsoft.Json](https://www.nuget.org/packages/Newtonsoft.Json) NuGet packages as reference to your .NET Standard applications from [NuGet.org](https://www.nuget.org). + +Step 4: Include the following namespaces in the **Program.cs** file. + +```csharp +using System.Data; +using Newtonsoft.Json; +using Syncfusion.XlsIO; +``` + +Step 5: Include the below code snippet in **Program.cs** to convert JSON document to CSV format document. +```csharp +//Load JSON file +string jsonPath = Path.GetFullPath("Data/Input.json"); +string jsonData = File.ReadAllText(jsonPath); + +//Deserialize to DataTable +DataTable dataTable = JsonConvert.DeserializeObject(jsonData); + +//Initialize ExcelEngine +using (ExcelEngine excelEngine = new ExcelEngine()) +{ + IApplication application = excelEngine.Excel; + application.DefaultVersion = ExcelVersion.Xlsx; + IWorkbook workbook = application.Workbooks.Create(1); + IWorksheet sheet = workbook.Worksheets[0]; + + //Import DataTable to worksheet + sheet.ImportDataTable(dataTable, true, 1, 1); + + //Saving the workbook as CSV + FileStream outputStream = new FileStream(Path.GetFullPath("Output/Sample.csv"), FileMode.Create, FileAccess.ReadWrite); + workbook.SaveAs(outputStream, ","); + + //Dispose streams + outputStream.Dispose(); +} +``` \ No newline at end of file