Convert Word Files to JSON Format via C#

Parse & Convert Word to JSON via C# without using Microsoft® Word

WORD Conversion via Python WORD Conversion via Java WORD Conversion via C++ WORD Conversion in Android Apps

 

Aspose.Total for .NET is a comprehensive suite of APIs that enables developers to easily convert Word documents to JSON format in their .NET, C#, ASP.NET, or VB.NET applications. This conversion process can be completed in two simple steps. Firstly, the Aspose.Words for .NET API is used to export the Word file to HTML. This API is a feature-rich library that enables the manipulation of Word documents in various formats, including DOC, DOCX, RTF, and ODT. It ensures that the document retains its formatting and structure. Secondly, the Aspose.Cells for .NET Spreadsheet Programming API is used to convert the HTML file to JSON. This API supports the conversion of HTML to various formats, including JSON. It also provides high-speed generation, manipulation, and rendering of spreadsheets in various formats, such as XLSX, XLS, XLSM, CSV, and TXT. By using Aspose.Total for .NET, developers can easily convert Word documents to JSON format with just two simple steps.

Convert Word to JSON via C#

  1. Load any Word document using Document class
  2. Convert Word to HTML by using Document.Save method
  3. Load HTML in an instance of Workbook class
  4. Save the result in JSON format using Workbook.Save method

Conversion Requirements

Install from the command line as nuget install Aspose.Total or via Package Manager Console of Visual Studio. Alternatively, get the offline MSI installer or DLLs in a ZIP file from downloads

// supports DOC, DOT, DOCX, DOCM, DOTX, DOTM, RTF, WordML, MOBI, ODT, and OTT file formats
// load DOCX as input file format with an instance of Document class
var document = new Document("template.docx");
// save document in HTML format
document.Save("html_output.html", Aspose.Words.SaveFormat.Html);
// load the HTML file in an instance of Workbook
var book = new Workbook("html_output.html");
// save input document as JSON
book.Save("output.json", SaveFormat.Json);

Convert Protected Word to JSON Format via C#

In addition to converting Word documents to JSON format, the Aspose.Total for .NET API also provides the ability to open password-protected documents. If your input Word document is password-protected, you’ll need to provide the correct password to convert it to JSON format. With the API, you can open the encrypted document by passing the password in a LoadOptions object. The code snippet below illustrates how you can attempt to open an encrypted document with a password.

// supports DOC, DOT, DOCX, DOCM, DOTX, and DOTM file formats
// load DOCX as input file format with an instance of Document class
var document = new Document("template.docx", new LoadOptions("MyPassword"));
// save document in HTML format
document.Save("html_output.html", Aspose.Words.SaveFormat.Html);
// load the HTML file in an instance of Workbook
var book = new Workbook("html_output.html");
// save input document as JSON
book.Save("output.json", SaveFormat.Json);

Convert Word to JSON in Range via C#

You can also specify a range for your output JSON. To do so, you can first convert the Word document to HTML using the API, and then open the resulting HTML file using the Workbook class. From there, you can retrieve the CellsCollection of the worksheet that contains the data, and create a range by specifying the row and column indices. Finally, you can call the ExportRangeToJson method with references to the Range and ExportRangeToJsonOptions objects to generate the JSON data, which can be saved to a file using the File.WriteAllText method.

// supports DOC, DOT, DOCX, DOCM, DOTX, DOTM, RTF, WordML, MOBI, ODT, and OTT file formats
// load DOCX as input file format with an instance of Document class
var document = new Document("template.docx");
// save document in HTML format
document.Save("html_output.html", Aspose.Words.SaveFormat.Html);
// load HTML file with an instance of Workbook
var workbook = new Cells.Workbook("output.html", new Cells.LoadOptions(Cells.LoadFormat.Auto));
// access CellsCollection of the worksheet containing data to be converted
var cells = workbook.Worksheets[0].Cells;
// create & set ExportRangeToJsonOptions for advanced options
var exportOptions = new Cells.Utility.ExportRangeToJsonOptions();
// create a range of cells containing data to be exported
var range = cells.CreateRange(0, 0, cells.LastCell.Row + 1, cells.LastCell.Column + 1);
// export range as JSON data
string jsonData = Cells.Utility.JsonUtility.ExportRangeToJson(range, exportOptions);
// write data file to disc in JSON format
System.IO.File.WriteAllText("output.json", jsonData);

Transforming WORD File to JSON Programmatically : Use Cases

Word to JSON Conversion Enables Unlocking of Full Potential of Data Visualization and Analysis Capabilities

The conversion of Word documents into JSON formats is necessary to unlock the full potential of your data visualization and analysis capabilities. This conversion enables you to:

Use Cases:

  • Data Storage and Sharing: Convert Word documents to JSON, enabling seamless storage and sharing of data across different platforms and devices.
  • Machine Learning Model Training: Use JSON to feed machine learning models with structured data, allowing for accurate predictions and informed decision-making.
  • Web Application Development: Convert Word documents to JSON, creating a robust foundation for web applications that can handle large amounts of data.
  • Scientific Research Collaboration: Use JSON to share research findings and collaborate with others in real-time, accelerating scientific discovery.
  • Business Intelligence Reporting: Convert Word documents to JSON, enabling the creation of interactive reports and dashboards that drive business insights.

Explore WORD Conversion Options with .NET

Convert WORD to EXCEL (Spreadsheet File Formats)