json to html

How to Convert JSON to HTML in Java.

Why Convert JSON to HTML?

JSON (JavaScript Object Notation) is a lightweight and widely used data format. However, for displaying data effectively in web applications, converting JSON to HTML is essential. This allows seamless integration of real-time data into web pages, enhancing user experience and interactivity.

JSON to HTML Conversion API

GroupDocs.Conversion Cloud SDK for Java is a robust and flexible REST architecture based API offering the capabilities to convert various file formats including JSON to HTML.

Install GroupDocs.Conversion Cloud SDK for Java

In order to use the SDK, the first step is to install the reference of GroupDocs.Conversion Cloud SDK for Java. Please add the following Maven dependency in pom.xml:

<repository>
    <id>repository.groupdocs.cloud</id>
    <name>repository.groupdocs.cloud</name>
    <url>https://releases.groupdocs.cloud/java/repo/</url>
</repository>
<dependency>
    <groupId>com.groupdocs</groupId>
    <artifactId>groupdocs-conversion-cloud</artifactId>
    <version>24.2.0</version>
</dependency>

Get API Credentials

Obtain your Client ID and Client Secret credentials from GroupDocs Cloud Dashboard by following the instructions specified in this tutorial.

Convert JSON to HTML in Java

Follow these steps to perform JSON to HTML conversion:

  1. Authenticate API Credentials.
Configuration configuration = new Configuration("your-client-id", "your-client-secret");
ConvertApi apiInstance = new ConvertApi(configuration);
  1. Upload JSON File to Cloud Storage.
FileUploadApi fileUpload = new FileUploadApi(configuration);
File inputFile = new File("source.json");
fileUpload.uploadFile(new UploadFileRequest("source.json", inputFile));
  1. Set Conversion Parameters.
ConvertSettings settings = new ConvertSettings();
settings.setFilePath("source.json");
settings.setFormat("html");
settings.setOutputPath("converted-html-file.html");
  1. Perform JSON to HTML conversion.
ConvertDocumentRequest request = new ConvertDocumentRequest(settings);
DocumentResult response = apiInstance.convertDocument(request);
System.out.println("Conversion successful! HTML saved at: " + response.getFilePath());
// More examples over https://github.com/groupdocs-conversion-cloud/groupdocs-conversion-cloud-dotnet
// Obtain your API credentials
string clientId = "XXXXXX-XXXXXXXXX-4088-9ca0-55c38f4b7f22";
string clientSecret1 = "XXXXXXXXXXXXXXXXXXXX";
// Create an instance of the Configuration class and initialize it with the Client ID & Client Secret
Configuration configuration = new Configuration(clientId, clientSecret);
// Define the API base URL for JSON file to HTML webpage conversion operation
configuration.setApiBaseUrl("https://api.groupdocs.cloud");
// Initialize an instance of ConvertApi with the Configuration object
ConvertApi convertApi = new ConvertApi(configuration);
ConvertSettings settings = new ConvertSettings();
settings.setStorageName("internal");
settings.setFilePath("source.json");
settings.setFormat("html");
settings.setOutputPath("myResultant.html");
// Invoke the ConvertDocument API for JSON file to HTML conversion
ConvertDocumentRequest request = new ConvertDocumentRequest(settings);
List<StoredConvertedResult> response = convertApi.convertDocument(request);
// Check the response and print success message
if (response != null && response.equals("OK")) {
System.out.println("Conversion successful! HTML saved at: " + response.getFilePath());
}
json to html

Image:- A preview of JSON to HTML conversion with Java.

The input JSON used in the above example can be downloaded from this link.

Convert JSON to Web Page with cURL

The conversion of JSON file to HTML document can be simplified using GroupDocs.Conversion Cloud and cURL commands.Its platform-independent and provides high-quality data transformation without requiring extensive coding.

To get started with this approach, we need to first generate a JWT access token based on client credentials. Once the JWT token is obtained, please execute the following cURL command to convert the JSON file to HTML format. After conversion, the resultant HTML is stored in cloud storage.

curl -v "https://api.groupdocs.cloud/v2.0/conversion" \
-X POST \
-H  "accept: application/json" \
-H  "authorization: Bearer {JWTtoken}" \
-H  "Content-Type: application/json" \
-d "{  \"StorageName\": \"internal\",  \"FilePath\": \"{inputFile}\",  \"Format\": \"html\",  \"ConvertOptions\": {    \"FromPage\": 1,    \"PagesCount\": 1,    \"Pages\": [      1    ]  },  \"OutputPath\": \"myConverted.html\"}"

Please replace inputFile with the name of input JSON file and JWTtoken with a personalized JWT access token.

  • In order to save the resultant HTML file on local drive, please skip the OutputPath parameter. Please use the following cURL command to accomplish this requirement:
curl -v "https://api.groupdocs.cloud/v2.0/conversion" \
-X POST \
-H "accept: application/json" \
-H "authorization: Bearer {accessToken}" \
-H "Content-Type: application/json" \
-d "{  \"StorageName\": \"internal\",  \"FilePath\": \"source.json\",  \"Format\": \"html\"}"

Try Our Free JSON to HTML Converter

Experience our free online JSON to HTML converter built using GroupDocs.Conversion Cloud API.

json to html converter

Conclusion

This article explored how to convert JSON to HTML using Java with GroupDocs.Conversion Cloud SDK. This approach allows developers to efficiently transform structured data into well-formatted, dynamic web content. By integrating this method, businesses can enhance data visualization and improve user engagement on their web applications.

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