Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save groupdocs-cloud-gists/ff1481e41bf71a912ceab9779a1ba536 to your computer and use it in GitHub Desktop.
Save groupdocs-cloud-gists/ff1481e41bf71a912ceab9779a1ba536 to your computer and use it in GitHub Desktop.
Convert EXCEL to JSON and JSON to EXCEL in Node.js
// How to Excel File to JSON Format Online in Node.js
const convert = async () => {
const convertApi = groupdocs_conversion_cloud.ConvertApi.fromKeys(clientId, clientSecret);
const settings = new groupdocs_conversion_cloud.ConvertSettings();
settings.storageName = myStorage;
settings.filePath = "nodejs-testing/sample-file.xlsx";
settings.format = "json";
settings.outputPath = "nodejs-testing/sample-file.json";
try {
// Create convert document request
const request = new groupdocs_conversion_cloud.ConvertDocumentRequest(settings);
await convertApi.convertDocument(request);
}
catch (err) {
throw err;
}
}
convert()
.then(() => {
console.log("Successfully converted Excel file to JSON format");
})
.catch((err) => {
console.log("Error occurred while converting the Excel file:", err);
})

You can convert Excel to JSON and JSON to Excel file programmatically on the cloud. In this article, you will learn how to convert xlsx to json and json to xlsx format online in Node.js using REST API.

The following topics shall be covered in this article:

  1. Excel to JSON and JSON to Excel Conversion REST API – Installation
  2. How to Convert Excel File to JSON Online in Node.js
  3. How to Convert JSON to Excel Online using Node.js
  4. Upload Files on the Cloud and then Download the Converted files
// How to Convert JSON to Excel Online using Node.js REST API
const convert = async () => {
const convertApi = groupdocs_conversion_cloud.ConvertApi.fromKeys(clientId, clientSecret);
const settings = new groupdocs_conversion_cloud.ConvertSettings();
settings.storageName = myStorage;
settings.filePath = "nodejs-testing/sample-file.json";
settings.format = "xlsx";
settings.outputPath = "nodejs-testing/sample-file.xlsx";
try {
// Create convert document request
const request = new groupdocs_conversion_cloud.ConvertDocumentRequest(settings);
await convertApi.convertDocument(request);
}
catch (err) {
throw err;
}
}
convert()
.then(() => {
console.log("Successfully converted JSON file to Excel XLSX format");
})
.catch((err) => {
console.log("Error occurred while converting the JSON file:", err);
})
// construct FileApi to download converted file
var fileApi = groupdocs_conversion_cloud.FileApi.fromConfig(config);
// create donwload file request
let request = new groupdocs_conversion_cloud.DownloadFileRequest("nodejs-testing/sample-file.json", myStorage);
// download file and response type Stream
fileApi.downloadFile(request)
.then(function (response) {
// save file in your system directory
fs.writeFile("H:\\groupdocs-cloud-data\\sample-file.json", response, "binary", function (err) { });
console.log("Expected response type is Stream: " + response.length);
})
.catch(function (error) {
console.log("Error: " + error.message);
});
// open file in IOStream from your system drive.
var resourcesFolder = 'H:\\groupdocs-cloud-data\\sample-file.xlsx';
// read file
fs.readFile(resourcesFolder, (err, fileStream) => {
// construct FileApi
var fileApi = groupdocs_conversion_cloud.FileApi.fromConfig(config);
// create upload file request
var request = new groupdocs_conversion_cloud.UploadFileRequest("sample-file.xlsx", fileStream, myStorage);
// upload file
fileApi.uploadFile(request)
.then(function (response) {
console.log("Expected response type is FilesUploadResult: " + response.uploaded.length);
})
.catch(function (error) {
console.log("Error: " + error.message);
});
});
# Import Node.js SDK in your node application from http://api.groupdocs.cloud
global.groupdocs_conversion_cloud = require("groupdocs-conversion-cloud");
global.fs = require("fs");
// get clientId and clientSecret from https://dashboard.groupdocs.cloud (free registration is required).
global.clientId = "xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
global.clientSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
global.myStorage = "test-internal-storage";
const config = new groupdocs_conversion_cloud.Configuration(clientId, clientSecret);
config.apiBaseUrl = "https://api.groupdocs.cloud";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment