Transforming JSON data into an Excel spreadsheet is a frequent need when reporting or analyzing structured information. Aspose.Words Cloud SDK for Java provides a powerful cloud‑based API that simplifies this conversion in Java applications. In this guide we will walk through how to convert JSON to XLSX in Java, covering a complete code example, the equivalent REST calls with cURL, and essential best‑practice recommendations. By the end you will be able to generate XLSX files from any JSON source and handle nested structures with ease.

Generate XLSX From JSON Using Java - Complete Code Example

This example demonstrates how to read a JSON file, build a DOCX table, and convert it to XLSX using Aspose.Words Cloud SDK for Java.

import com.aspose.words.cloud.*;
import com.aspose.words.cloud.model.*;
import com.aspose.words.cloud.api.*;
import com.aspose.words.cloud.auth.*;
import com.fasterxml.jackson.databind.*;
import java.io.*;
import java.util.*;

public class JsonToXlsxConverter {
    public static void main(String[] args) throws Exception {
        // Initialize API
        String clientId = "YOUR_CLIENT_ID";
        String clientSecret = "YOUR_CLIENT_SECRET";
        WordsApi wordsApi = new WordsApi(clientId, clientSecret);
        
        // Paths
        String jsonPath = "data.json";
        String tempDocxPath = "temp.docx";
        String outputXlsxPath = "output.xlsx";
        
        // Read JSON
        ObjectMapper mapper = new ObjectMapper();
        List<Map<String, Object>> records = mapper.readValue(new File(jsonPath),
                mapper.getTypeFactory().constructCollectionType(List.class, Map.class));
        
        // Define column order (keys from first record)
        List<String> columns = new ArrayList<>(records.get(0).keySet());
        
        // Create empty DOCX in cloud
        CreateDocumentRequest createRequest = new CreateDocumentRequest("temp.docx");
        wordsApi.createDocument(createRequest);
        
        // Build table rows
        TableRowInsert requestRow = new TableRowInsert();
        // First row - header
        TableRow headerRow = new TableRow();
        List<TableCell> headerCells = new ArrayList<>();
        for (String col : columns) {
            TableCell cell = new TableCell();
            cell.setText(col);
            headerCells.add(cell);
        }
        headerRow.setTableCellList(headerCells);
        // Insert header row at position 0
        InsertTableRowRequest insertHeader = new InsertTableRowRequest("temp.docx", 0, headerRow);
        wordsApi.insertTableRow(insertHeader);
        
        // Insert data rows
        int rowIndex = 1;
        for (Map<String, Object> rec : records) {
            TableRow dataRow = new TableRow();
            List<TableCell> dataCells = new ArrayList<>();
            for (String col : columns) {
                TableCell cell = new TableCell();
                cell.setText(String.valueOf(rec.get(col)));
                dataCells.add(cell);
            }
            dataRow.setTableCellList(dataCells);
            InsertTableRowRequest insertRow = new InsertTableRowRequest("temp.docx", rowIndex, dataRow);
            wordsApi.insertTableRow(insertRow);
            rowIndex++;
        }
        
        // Convert DOCX to XLSX
        ConvertDocumentRequest convertRequest = new ConvertDocumentRequest("temp.docx", "xlsx");
        byte[] xlsxBytes = wordsApi.convertDocument(convertRequest);
        try (FileOutputStream fos = new FileOutputStream(outputXlsxPath)) {
            fos.write(xlsxBytes);
        }
    }
}

Note: This code example demonstrates the core functionality. Before using it in your project, make sure to update any file paths and configuration values to match your actual environment, verify that all required dependencies are properly installed, and test thoroughly in your development environment. If you encounter any issues, please refer to the official documentation or reach out to the support team for assistance.

JSON To XLSX Conversion Via REST API Using cURL

The same conversion can be performed without writing Java code by calling the Aspose.Words Cloud REST API directly.

  1. Obtain an access token - the API uses OAuth 2.0.
curl -X POST "https://api.aspose.cloud/connect/token" \
     -H "Content-Type: application/x-www-form-urlencoded" \
     -d "grant_type=client_credentials&client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET"
  1. Upload the JSON file (the API expects a DOCX source, so we first upload a placeholder DOCX).
curl -X PUT "https://api.aspose.cloud/v4.0/words/temp.docx" \
     -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
     -H "Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document" \
     --data-binary "@temp.docx"
  1. Create a table from JSON - this step is handled by the SDK in code; with the REST API you would upload a pre‑filled DOCX that already contains the table.

  2. Convert the DOCX to XLSX.

curl -X GET "https://api.aspose.cloud/v4.0/words/temp.docx?format=xlsx" \
     -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
     -o output.xlsx

For a complete list of parameters, see the official API documentation.

Convert JSON To XLSX in Java - How the Code Works

Below is a concise breakdown of the key sections in the Java example.

  1. API Initialization - WordsApi wordsApi = new WordsApi(clientId, clientSecret); creates an authenticated client.
WordsApi wordsApi = new WordsApi(clientId, clientSecret);
  1. Reading JSON - ObjectMapper deserialises the JSON file into a list of maps, preserving the original field order.
ObjectMapper mapper = new ObjectMapper();
List<Map<String, Object>> records = mapper.readValue(
        new File(jsonPath),
        mapper.getTypeFactory().constructCollectionType(List.class, Map.class));
  1. Creating a DOCX in the cloud - CreateDocumentRequest creates an empty DOCX that will host the table.
CreateDocumentRequest createRequest = new CreateDocumentRequest("temp.docx");
wordsApi.createDocument(createRequest);
  1. Building the header row - The first record’s keys define the column order, and each key becomes a header cell.
TableRow headerRow = new TableRow();
List<TableCell> headerCells = new ArrayList<>();
for (String col : columns) {
    TableCell cell = new TableCell();
    cell.setText(col);
    headerCells.add(cell);
}
headerRow.setTableCellList(headerCells);
InsertTableRowRequest insertHeader = new InsertTableRowRequest("temp.docx", 0, headerRow);
wordsApi.insertTableRow(insertHeader);
  1. Inserting data rows - Each JSON object is turned into a TableRow where every cell receives the string representation of the corresponding value.
for (Map<String, Object> rec : records) {
    TableRow dataRow = new TableRow();
    List<TableCell> dataCells = new ArrayList<>();
    for (String col : columns) {
        TableCell cell = new TableCell();
        cell.setText(String.valueOf(rec.get(col)));
        dataCells.add(cell);
    }
    dataRow.setTableCellList(dataCells);
    InsertTableRowRequest insertRow = new InsertTableRowRequest("temp.docx", rowIndex, dataRow);
    wordsApi.insertTableRow(insertRow);
    rowIndex++;
}
  1. Conversion to XLSX - ConvertDocumentRequest tells the service to transform the temporary DOCX into an XLSX file, which is then written locally.
ConvertDocumentRequest convertRequest = new ConvertDocumentRequest("temp.docx", "xlsx");
byte[] xlsxBytes = wordsApi.convertDocument(convertRequest);

For detailed API reference, visit the Aspose.Words Cloud API Reference.

Installing and Configuring Aspose.Words Cloud SDK for Java

Add the Maven dependency to your project’s pom.xml:

<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-words-cloud</artifactId>
    <version>26.9.0</version>
</dependency>

Download the latest JARs from the download page if you prefer manual installation.
Prerequisites:

  • Java 8 or higher.
  • An Aspose Cloud account with client ID and client secret.
  • Network access to api.aspose.cloud.

Configure the SDK by setting the clientId and clientSecret variables as shown in the code example.

Conclusion

Converting JSON to XLSX in Java becomes straightforward with the Aspose.Words Cloud SDK for Java. The provided example walks you through reading JSON, constructing a DOCX table, and converting it to an XLSX workbook, while the cURL section shows a language‑agnostic alternative. Remember to secure a proper license for production use; you can view pricing on the product page and obtain a temporary license from the temporary license page. With these tools in hand, you can automate Excel report generation from any JSON source quickly and reliably.

FAQs

How do I convert JSON to XLSX in Java using Aspose.Words Cloud SDK for Java?
Use the code sample above or the REST API. The SDK reads JSON, creates a DOCX table, and then converts it to XLSX with a single API call.

Can the SDK handle nested JSON structures?
Yes. Flatten nested objects into separate columns before inserting rows, as demonstrated in the “Practical Tips” section.

Do I need a license for production deployments?
A valid license is required for production. Purchase options are listed on the product page, and a temporary license is available for testing at the temporary license page.

Is there a way to perform the conversion without writing Java code?
Absolutely. The cURL example shows how to achieve the same result via the REST API, which can be called from any platform.

Read More