By using Aspose.Total for Java you can convert JSON to WORDML in your Java applications in two-step process. Firstly, by using Aspose.Cells for Java you can parse JSON to PDF. In the second step, you can convert PDF to WORDML by using Word Processing API Aspose.Words for Java .
Convert JSON Format to WORDML via Java
- Create a new Workbook object and read valid JSON data from file
- Import JSON file to worksheet using JsonUtility class and Save it as PDF
- Load PDF document by using Document class
- Save the document to WORDML format using Save method
Conversion Requirements
You can easily use Aspose.Total for Java directly from a Maven based project and include libraries in your pom.xml.
Alternatively, you can get a ZIP file from downloads .
// create a blank Workbook object | |
Workbook workbook = new Workbook(); | |
// access default empty worksheet | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Read JSON file | |
String jsonInput = new String(Files.readAllBytes("SampleJson.json")); | |
// import JSON data to default worksheet starting at cell A1 | |
JsonUtility.importData(jsonInput, worksheet.getCells(), 0, 0, new JsonLayoutOptions()); | |
// save resultant file in JSON-TO-PDF format | |
workbook.save("output.pdf", SaveFormat.AUTO); | |
// load PDF with an instance of Document | |
var document = new Document("output.pdf"); | |
// supports DOC, DOT, DOCM, DOTX, DOTX, FLATOPC, RTF, WordML, MOBI, CHM, ODT, OTT, PS, PCL, EPUB file formats | |
// call Save method while passing SaveFormat.DOC | |
document.Save("output.doc", SaveFormat.Doc); |
Set Layout & Convert JSON Format to WORDML via Java
Furthermore, the API allows you to set layout options for your JSON while parsing JSON to WORDML using JsonLayoutOptions . It allows you to process Array as a table, ignore nulls, ignore array title, ignore object title, convert string to number or date, set date and number format, and set title style. All of these options allow you to present your data as per your needs. The following code snippet shows you how to set the layout options.
// create a blank Workbook object | |
Workbook workbook = new Workbook(); | |
// access default empty worksheet | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// read JSON file | |
String jsonInput = new String(Files.readAllBytes("SampleJson.json")); | |
// set JsonLayoutOptions for formatting | |
JsonLayoutOptions layoutOptions = new JsonLayoutOptions(); | |
layoutOptions.setArrayAsTable(true); | |
layoutOptionssetConvertNumericOrDate(true); | |
layoutOptionssetIgnoreArrayTitle(true); | |
layoutOptionssetIgnoreNull(true); | |
layoutOptionssetIgnoreObjectTitle(true); | |
// import JSON data to default worksheet starting at cell A1 | |
JsonUtility.importData(jsonInput, worksheet.getCells(), 0, 0, layoutOptions); | |
// save resultant file in JSON-TO-PDF format | |
workbook.save("output.pdf", SaveFormat.AUTO); | |
// load PDF with an instance of Document | |
var document = new Document("output.pdf"); | |
// supports DOC, DOT, DOCM, DOTX, DOTX, FLATOPC, RTF, WordML, MOBI, CHM, ODT, OTT, PS, PCL, EPUB file formats | |
// call Save method while passing SaveFormat.DOC | |
document.Save("output.doc", SaveFormat.Doc); |
Convert JSON Format to WORDML with Watermark via Java
Using the API, you can also parse JSON to WORDML with watermark. In order to add a watermark to your WORDML document, you can first convert the JSON file to PDF and add a watermark to it. In order to add a watermark, load the newly created PDF file using the Document class, create an instance of TextWatermarkOptions and set its properties, Call Watermark.setText method and pass watermark text & object of TextWatermarkOptions. After adding the watermark, you can save the document to WORDML.
// create a blank Workbook object | |
Workbook workbook = new Workbook(); | |
// access default empty worksheet | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Read JSON file | |
String jsonInput = new String(Files.readAllBytes("SampleJson.json")); | |
// import JSON data to default worksheet starting at cell A1 | |
JsonUtility.importData(jsonInput, worksheet.getCells(), 0, 0, new JsonLayoutOptions()); | |
// save resultant file in JSON-TO-PDF format | |
workbook.save("output.pdf", SaveFormat.AUTO); | |
// load PDF with an instance of Document | |
var document = new Document("output.pdf"); | |
TextWatermarkOptions options = new TextWatermarkOptions(); | |
options.setFontFamily("Arial"); | |
options.setFontSize(36); | |
options.setColor(Color.BLACK); | |
options.setLayout(WatermarkLayout.HORIZONTAL); | |
options.isSemitrasparent(false); | |
document.getWatermark().setText("Test", options); | |
// supports DOC, DOT, DOCM, DOTX, DOTX, FLATOPC, RTF, WordML, MOBI, CHM, ODT, OTT, PS, PCL, EPUB file formats | |
// call Save method while passing SaveFormat.DOC | |
document.Save("output.doc", SaveFormat.Doc); |
Explore JSON Conversion Options with Java
What is JSON File Format?
The JSON (JavaScript Object Notation) file format is a lightweight and widely used data interchange format. It was derived from the JavaScript programming language but is now language-independent and supported by various programming languages. JSON files store data in a structured and readable format, making them easy to understand and process by both humans and machines.
JSON files consist of key-value pairs organized in a hierarchical structure. They represent data in a simple and intuitive way using objects (enclosed in curly braces {}) and arrays (enclosed in square brackets []). Each key is paired with a corresponding value, which can be a string, number, boolean, null, object, or array. This flexibility allows JSON to handle complex and nested data structures.
One of the main advantages of JSON is its simplicity and ease of use. Its lightweight nature and minimal syntax make it efficient for data transmission over networks and storage in files. JSON files are commonly used for data exchange between web servers and clients, as well as for configuration files, APIs, and storing structured data.
JSON files are human-readable and can be easily understood and modified using a text editor. They are also machine-readable, allowing applications to parse and process JSON data efficiently. Many programming languages provide built-in libraries or packages for working with JSON, simplifying the parsing and serialization of JSON data.
What is WORDML File Format?
Microsoft Word 2003 introduced a proprietary document format known as WordprocessingML. This format, based on XML, utilizes a custom XML schema to store various elements of a Word document, encompassing text, formatting, images, and other content. WordprocessingML aims to provide a comprehensive representation of all document information while maintaining both human-readable and machine-readable qualities.
Being human-readable means that WordprocessingML documents can be easily accessed and edited using a standard text editor. This accessibility allows users to directly manipulate the underlying XML structure if needed. Additionally, WordprocessingML is designed to facilitate automated processing and conversion by computer systems, enabling seamless transformation into other formats or integration with different software applications.
WordprocessingML is an open standard, meaning that its specifications are publicly available for reference and implementation by developers. This openness has fostered the development of numerous software programs, including open-source solutions like OpenOffice.org and LibreOffice, which can read and write WordprocessingML documents. The availability of such tools expands the accessibility and interoperability of WordprocessingML files beyond the Microsoft Word ecosystem.
Microsoft Word, being one of the most widely used word processing programs worldwide, has contributed to the popularity of WordprocessingML as an XML-based word processing format. Its broad acceptance has established WordprocessingML as a prevalent standard for sharing and exchanging richly formatted documents.