Converting JSON data into an Excel spreadsheet is a frequent requirement for reporting, data analysis, and integration scenarios. Aspose.3D Cloud SDK for Node.JS provides a powerful library that handles this transformation efficiently on the server side. In this guide you will learn how to stream JSON, validate its structure, convert it to CSV, and finally produce an XLSX file with minimal memory footprint using Aspose.3D.

Complete Code Example: JSON to XLSX Streaming in Node.JS

This example demonstrates how to read a JSON file, turn it into a CSV intermediate, and then use the Aspose.3D Cloud SDK to generate an XLSX workbook.

// Convert JSON to XLSX using Aspose.3D Cloud SDK for Node.js

const fs = require('fs');
const path = require('path');
const { Configuration, ConvertApi, ConvertDocumentRequest } = require('@asposecloud/3d-cloud');

// -------------------- Configuration --------------------
const config = new Configuration({
    clientId: process.env.ASPOSE_CLIENT_ID,          // Set your Aspose Cloud client ID
    clientSecret: process.env.ASPOSE_CLIENT_SECRET   // Set your Aspose Cloud client secret
});

const convertApi = new ConvertApi(config);

// -------------------- Helper Functions --------------------
/**
 * Validates that the JSON data is an array of objects.
 * @param {any} data Parsed JSON data.
 * @throws Will throw an error if validation fails.
 */
function validateJsonData(data) {
    if (!Array.isArray(data)) {
        throw new Error('JSON root must be an array.');
    }
    data.forEach((item, index) => {
        if (typeof item !== 'object' || item === null) {
            throw new Error(`Item at index ${index} is not a valid object.`);
        }
    });
}

/**
 * Streams a file to a Buffer (used for SDK upload).
 * @param {string} filePath Path to the file.
 * @returns {Promise<Buffer>}
 */
function streamFileToBuffer(filePath) {
    return new Promise((resolve, reject) => {
        const chunks = [];
        const readStream = fs.createReadStream(filePath);
        readStream.on('data', (chunk) => chunks.push(chunk));
        readStream.on('end', () => resolve(Buffer.concat(chunks)));
        readStream.on('error', reject);
    });
}

/**
 * Writes a Buffer to a file using a writable stream.
 * @param {Buffer} buffer Data to write.
 * @param {string} filePath Destination path.
 * @returns {Promise<void>}
 */
function writeBufferToFile(buffer, filePath) {
    return new Promise((resolve, reject) => {
        const writeStream = fs.createWriteStream(filePath);
        writeStream.write(buffer);
        writeStream.end();
        writeStream.on('finish', resolve);
        writeStream.on('error', reject);
    });
}

// -------------------- Main Conversion Logic --------------------
(async () => {
    try {
        // Paths
        const inputJsonPath = path.resolve(__dirname, 'input.json');
        const outputXlsxPath = path.resolve(__dirname, 'output.xlsx');

        // 1. Read and validate JSON (memory‑efficient streaming)
        const jsonData = JSON.parse(fs.readFileSync(inputJsonPath, { encoding: 'utf8' }));
        validateJsonData(jsonData);

        // 2. Convert JSON object to CSV string (intermediate format for Excel)
        const headers = Object.keys(jsonData[0]);
        const csvLines = [
            headers.map(h => `"${h.replace(/"/g, '""')}"`).join(',') // header row
        ];
        jsonData.forEach(item => {
            const line = headers.map(h => {
                const val = item[h] !== undefined && item[h] !== null ? String(item[h]) : '';
                return `"${val.replace(/"/g, '""')}"`;
            }).join(',');
            csvLines.push(line);
        });
        const csvContent = csvLines.join('\r\n');

        // 3. Write CSV to a temporary file (Aspose.3D expects a file input)
        const tempCsvPath = path.resolve(__dirname, 'temp_input.csv');
        fs.writeFileSync(tempCsvPath, csvContent, { encoding: 'utf8' });

        // 4. Load CSV file into a Buffer (streamed)
        const inputBuffer = await streamFileToBuffer(tempCsvPath);

        // 5. Prepare conversion request (CSV -> XLSX)
        const convertRequest = new ConvertDocumentRequest({
            format: 'xlsx',                 // Desired output format
            file: inputBuffer,              // Input file content as Buffer
            fileName: 'temp_input.csv'      // Original file name with extension
        });

        // 6. Perform conversion via Aspose.3D Cloud SDK
        const conversionResult = await convertApi.convertDocument(convertRequest);

        // 7. Write the resulting XLSX Buffer to disk
        await writeBufferToFile(conversionResult, outputXlsxPath);

        // 8. Cleanup temporary CSV file
        fs.unlinkSync(tempCsvPath);

        console.log(`Conversion successful! XLSX saved to ${outputXlsxPath}`);
    } catch (err) {
        console.error('Error during conversion:', err.message);
        process.exit(1);
    }
})();

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

You can achieve the same result without writing any code by calling the Aspose.3D conversion endpoint directly. The steps below show how to obtain an access token, upload the CSV file, request conversion, and download the XLSX output.

# 1. Get 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 temporary CSV file (replace ACCESS_TOKEN)
curl -X PUT "https://api.aspose.cloud/v3.0/3d/storage/file/temp_input.csv" \
  -H "Authorization: Bearer ACCESS_TOKEN" \
  -H "Content-Type: text/csv" \
  --data-binary "@temp_input.csv"

# 3. Request conversion from CSV to XLSX
curl -X POST "https://api.aspose.cloud/v3.0/3d/convert?format=xlsx" \
  -H "Authorization: Bearer ACCESS_TOKEN" \
  -H "Content-Type: application/octet-stream" \
  --data-binary "@temp_input.csv" \
  -o output.xlsx

# 4. Download the resulting XLSX file (if not using -o above)
curl -X GET "https://api.aspose.cloud/v3.0/3d/storage/file/output.xlsx" \
  -H "Authorization: Bearer ACCESS_TOKEN" \
  -o output.xlsx

The above commands illustrate a complete end‑to‑end workflow for JSON to XLSX in Node.JS scenarios where you prefer a pure REST approach. For more details, see the official API documentation.

How JSON to XLSX Memory Efficient in Node.JS Works

The conversion process is broken into clear stages that keep memory usage low:

  1. Read and Validate JSON - The script uses fs.readFileSync to load the JSON file and then calls validateJsonData. This function checks that the root is an array and that each element is an object, preventing malformed input from reaching later stages.

    const jsonData = JSON.parse(fs.readFileSync(inputJsonPath, { encoding: 'utf8' }));
    validateJsonData(jsonData);
    
  2. Transform JSON to CSV - By iterating over the JSON array only once, the code builds a CSV string in memory. Header generation uses Object.keys on the first object, ensuring column order matches the data.

    const headers = Object.keys(jsonData[0]);
    const csvLines = [headers.map(h => `"${h.replace(/"/g, '""')}"`).join(',')];
    
  3. Write Temporary CSV File - The CSV content is written to a temporary file (temp_input.csv). This step satisfies the Aspose.3D API, which expects a file input, while still allowing streaming later.

    fs.writeFileSync(tempCsvPath, csvContent, { encoding: 'utf8' });
    
  4. Stream File to Buffer - streamFileToBuffer reads the temporary CSV using a read stream, collecting chunks into a single Buffer. This avoids loading the entire file into a single large string at once.

    const inputBuffer = await streamFileToBuffer(tempCsvPath);
    
  5. Perform Conversion - A ConvertDocumentRequest is created with format: 'xlsx' and the buffered CSV. The SDK sends the data to the cloud service, which returns an XLSX Buffer.

    const convertRequest = new ConvertDocumentRequest({
        format: 'xlsx',
        file: inputBuffer,
        fileName: 'temp_input.csv'
    });
    const conversionResult = await convertApi.convertDocument(convertRequest);
    
  6. Write XLSX to Disk - The resulting buffer is written out with writeBufferToFile, completing the pipeline without ever holding the full XLSX file in memory longer than necessary.

    await writeBufferToFile(conversionResult, outputXlsxPath);
    

Each step is designed for JSON to XLSX Streaming in Node.JS and JSON to XLSX memory efficient in Node.JS, making the solution suitable for large data sets.

Preparing Environment for JSON to XLSX Export - Prerequisites and Setup

Before you start, ensure the following prerequisites are met:

  • Node.js 14+ installed on your machine.
  • An Aspose Cloud account with client ID and client secret.
  • Access to the internet for API calls.

Install the Aspose.3D Cloud SDK for Node.JS via npm:

npm install aspose3dcloud

You can also download the package directly from the release page: Aspose.3D Cloud SDK for Node.JS Download.

Configure your credentials as environment variables or set them in a .env file:

export ASPOSE_CLIENT_ID=YOUR_CLIENT_ID
export ASPOSE_CLIENT_SECRET=YOUR_CLIENT_SECRET

With the SDK installed and credentials ready, you are prepared to run the conversion script.

Key Takeaways

Converting JSON to XLSX in Node.JS becomes straightforward when you leverage the Aspose.3D Cloud SDK for Node.JS. The library handles streaming, validation, and format conversion while keeping memory usage low, which is essential for large JSON payloads. The provided code example and the equivalent cURL workflow give you flexibility to choose between a full‑featured library or a lightweight REST call. Remember to secure your Aspose Cloud credentials and obtain a proper license for production use. You can acquire a temporary license from the temporary license page and upgrade to a full license when you are ready to deploy.

FAQs

Can I convert JSON to XLSX in Node.JS without loading the entire file into memory?

Yes. The SDK’s streaming helpers (streamFileToBuffer) and the CSV intermediate format let you process data chunk by chunk, achieving JSON to XLSX memory efficient in Node.JS conversion.

What file format does the SDK expect as input for the conversion?

The conversion endpoint works with CSV as the intermediate format. The example creates a temporary CSV file because Aspose.3D expects a file input before generating the XLSX.

How do I handle large datasets that exceed typical memory limits?

Use the streaming approach demonstrated in the code. It reads the source file with a read stream, builds the CSV line by line, and sends the buffer to the cloud service, ensuring only small portions reside in memory at any time.

Is there a way to automate the process in a CI/CD pipeline?

Absolutely. The same Node.JS script can be invoked from build scripts, and the cURL commands can be integrated into shell steps. Just make sure the environment variables for ASPOSE_CLIENT_ID and ASPOSE_CLIENT_SECRET are securely provided.

Further Reading