Exporting data from JSON files to Excel spreadsheets is a frequent requirement for reporting and data exchange in modern Node.JS applications. Aspose.OCR Cloud SDK for Node.js provides a powerful API that can handle this conversion directly in the cloud. In this guide, you will learn how to convert JSON to XLSX in Node.JS using async/await patterns and the SDK’s convertDocument method. We’ll walk through the prerequisites, a detailed code walkthrough, a complete example, and even a cURL alternative for REST integration.

Before You Start: Prerequisites and Installation

To follow this tutorial you need:

  • Node.js 14 or later installed locally.
  • An Aspose Cloud account with OCR API enabled.
  • Your Client Id and Client Secret from the Aspose Cloud dashboard.
  • Access to the file system where the JSON source and XLSX output will reside.

Install the SDK via npm:

npm install aspose-ocr-cloud

Download the latest package from the official release page: Aspose.OCR Cloud SDK for Node.js Download. After installation, you can start coding.

The following snippet shows how to configure the OCR client with your credentials:

const { OcrApi, Configuration } = require('asposeocrcloud');

const config = new Configuration({
    clientId: 'YOUR_CLIENT_ID',
    clientSecret: 'YOUR_CLIENT_SECRET'
});
const ocrApi = new OcrApi(config);

With the client ready, the next sections demonstrate the conversion workflow step by step.

Step-by-Step Guide to Convert JSON to XLSX in Node.JS

Below is a granular walk‑through of each operation required to convert JSON to XLSX in Node.JS.

Step 1: Load the Source Document

First, create a read stream for the JSON file you want to convert.

const fs = require('fs');
const path = require('path');

const inputJsonPath = path.resolve(__dirname, 'input.json');
const jsonFileStream = fs.createReadStream(inputJsonPath);

Step 2: Configure the OCR API Client

Reuse the configuration shown earlier to instantiate the API object.

const { OcrApi, Configuration } = require('asposeocrcloud');

const config = new Configuration({
    clientId: 'YOUR_CLIENT_ID',
    clientSecret: 'YOUR_CLIENT_SECRET'
});
const ocrApi = new OcrApi(config);

Step 3: Call the ConvertDocument Method

Invoke the conversion endpoint, specifying xlsx as the target format.

const response = await ocrApi.convertDocument({
    format: 'xlsx',
    file: jsonFileStream
});

The SDK returns the binary XLSX data in response.body.

Step 4: Write the XLSX Output File

Persist the binary data to a file on disk.

const outputXlsxPath = path.resolve(__dirname, 'output.xlsx');
fs.writeFileSync(outputXlsxPath, response.body, { encoding: 'binary' });
console.log('Conversion completed successfully:', outputXlsxPath);

Step 5: Clean Up Resources

Close the file stream to free system resources.

jsonFileStream.close();

With these steps, you have successfully performed a convert JSON to XLSX in Node.JS operation using the Aspose OCR Cloud library.

JSON to XLSX Conversion in Node.JS - Complete Code Example

The example below demonstrates the full implementation for converting JSON to XLSX in Node.JS using Aspose.OCR Cloud SDK.

const fs = require('fs');
const path = require('path');
const { OcrApi, Configuration } = require('asposeocrcloud');

(async () => {
    // Configure Aspose OCR Cloud client
    const config = new Configuration({
        clientId: 'YOUR_CLIENT_ID',
        clientSecret: 'YOUR_CLIENT_SECRET'
    });
    const ocrApi = new OcrApi(config);

    // Define input JSON and output XLSX paths
    const inputJsonPath = path.resolve(__dirname, 'input.json');
    const outputXlsxPath = path.resolve(__dirname, 'output.xlsx');

    // Prepare file stream for the JSON file
    const jsonFileStream = fs.createReadStream(inputJsonPath);

    try {
        // Convert JSON to XLSX using Aspose OCR Cloud SDK
        const response = await ocrApi.convertDocument({
            format: 'xlsx',
            file: jsonFileStream
        });

        // Write the returned binary data to the XLSX file
        fs.writeFileSync(outputXlsxPath, response.body, { encoding: 'binary' });

        console.log('Conversion completed successfully:', outputXlsxPath);
    } catch (error) {
        console.error('Conversion failed:', error);
    } finally {
        // Ensure the file stream is closed
        jsonFileStream.close();
    }
})();

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.

cURL Approach for JSON to XLSX Conversion in Node.JS

If you prefer a pure REST solution, the same conversion can be performed with cURL commands. This method also uses the primary phrase convert JSON to XLSX in Node.JS.

  1. Obtain an access token

    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"
    
  2. Upload the JSON file and request conversion

    curl -X POST "https://api.aspose.cloud/v3.0/ocr/convert?format=xlsx" \
         -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
         -F "file=@input.json"
    

    The response contains the binary XLSX payload.

  3. Save the XLSX output locally

    curl -X GET "https://api.aspose.cloud/v3.0/ocr/convert/result/output.xlsx" \
         -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
         -o output.xlsx
    

These commands illustrate how to achieve the same result without writing any Node.js code. For more details, consult the official API documentation.

Conclusion

Converting JSON to XLSX in Node.JS becomes straightforward when you leverage the Aspose.OCR Cloud SDK for Node.js. The SDK handles the heavy lifting, allowing you to focus on business logic rather than file format intricacies. Remember to secure your client credentials, handle errors gracefully, and test with realistic data sizes. For production deployments you’ll need a paid subscription; pricing details are available on the product page, and a temporary license can be obtained from the temporary license page. Happy coding!

FAQs

How do I convert JSON to XLSX in Node.JS using Aspose.OCR Cloud SDK?
Use the OcrApi.convertDocument method with format: 'xlsx'. The complete code example in this article shows the exact implementation steps.

What authentication is required for the OCR Cloud API?
Create a client ID and client secret in the Aspose Cloud dashboard, then exchange them for an OAuth token. The SDK’s Configuration object manages token retrieval automatically.

Can I process large JSON files efficiently?
Yes. The SDK streams the input file, keeping memory usage low. Combine this with async/await to keep the Node.js event loop responsive.

Where can I find more examples and support?
Visit the official GitHub repository, the Aspose documentation site, or ask questions on the support forum.

Read More