Excel to JSON

Convert Excel to JSON using Java

Excel is most widely used by businesses to analyze their data effectively. Another reason for its popularity is that it supports the creation of charts, graphs, and various visualizations that help in understand the data in more effective manner. However, if we need to share the data of excel worksheet with other applications, we need to parse its contents and share them in common format. Therefore, we may consider an option to export Excel to JSON format, as its commonly used to store and exchange data over the internet. In this article, we are going to explore the details on Excel to JSON conversion.

Excel to JSON Conversion API

Aspose.Cells Cloud SDK for Java is primarily developed to support the capabilities of Excel workbook creation, manipulation and transformation to formats such as HTML, PDF, JPG and other formats. Now in order to get started, the first step is to add the following details in pom.xml of maven build type project.

<repositories> 
    <repository>
        <id>aspose-cloud</id>
        <name>artifact.aspose-cloud-releases</name>
        <url>https://artifact.aspose.cloud/repo</url>
    </repository>   
</repositories>

<dependencies>
    <dependency>
        <groupId>com.aspose</groupId>
        <artifactId>aspose-cells-cloud</artifactId>
        <version>22.8</version>
    </dependency>
</dependencies>

Once the SDK reference has been added, we need to create a free account over Aspose Cloud. Login using newly created account and lookup/create Client ID and Client Secret at Cloud Dashboard. These details are required in subsequent sections.

Convert Excel to JSON using Java

We are going to discuss the details of excel to JSON conversion using Java code snippet. So please follow the instructions specified below to accomplish this requirement.

  • Firstly, create a CellsApi object while providing ClientID and Client secret details as arguments
  • Secondly, create a File instance to load input Excel workbook
  • Thirdly, call the uploadFile(…) method to upload the Excel workbook to cloud storage
  • Now call the cellsWorkbookGetWorkbook(…) method requiring input Excel name, resultant format as Json and the name of output file. After successful conversion, the JSON file is stored in cloud storage

Export Excel to JSON without Upload

In this section, we are going to discuss the details on how to convert Excel to JSON without first uploading the input Excel to cloud storage. However, the resultant JSON file will be stored in cloud storage.

  • Firstly, create a CellsApi object while providing ClientID and Client secret details as arguments
  • Secondly, create a File instance to load input Excel workbook
  • Thirdly, call the cellsWorkbookPutConvertWorkbook(…) method requiring input Excel name, resultant format as Json and the name of output file. After successful conversion, the JSON file is stored in cloud storage
Excel to JSON preview

Image1:- Excel to JSON Conversion Preview

The input excel workbook and the resultant Json file generated in above example can be downloaded from Testcase.xlsx and output.json

XLSX to JSON using cURL Commands

Since REST APIs can easily be accessed via cURL commands, so in this section, we are going to call Aspose.Cells Cloud via cURL commands to accomplish our requirement. Now one of the pre-requisite for this operation is to generate a JWT access token (based on client credentials) while executing the following command.

curl -v "https://api.aspose.cloud/connect/token" \
-X POST \
-d "grant_type=client_credentials&client_id=bb959721-5780-4be6-be35-ff5c3a6aa4a2&client_secret=4d84d5f6584160cbd91dba1fe145db14" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json"

Now once we have the JWT token, we need to execute the following command to convert Excel to JSON. The resultant JSON file will be stored in cloud storage.

curl -v -X GET "https://api.aspose.cloud/v3.0/cells/TestCase-original.xlsx?format=JSON&isAutoFit=false&onlySaveTable=false&outPath=output.json&checkExcelRestriction=true" \
-H  "accept: application/json" \
-H  "authorization: Bearer <JWT Token>"

Conclusion

In this article, we have Learned the conversion of Excel to JSON file using Java Cloud SDK. So we have witnessed that with fewer code lines, we have managed to accomplish our requirementAt the same time, we have explored the option for converting Excel to JSON with cURL command over the terminal. Apart from these approaches, another easiest way of exploring the API within web browser is through swagger interface. Also, please note that all our Cloud SDKs are built under MIT license, so the complete source code can be downloaded from GitHub. You may consider approaching us for a quick resolution of issues via free product support forum.

We highly recommend visiting the following links to learn more about: