How to Validate JSON File using Java

Since JSON files are one of the most common, interoperable data-transfer file formats available to us, it’s critically important to ensure those files are correctly structured before unwittingly creating bigger data processing problems down the line. Our JSON Validation API will let you know instantaneously if a JSON file was password-protected or invalid, and if invalid, detailed information including the description, path and URI will be provided. To help you structure an API call in Java, we’ve provided ready-to-run code examples and instructions below. At the bottom of this article, I’ve copied an example response model for your reference as well.

Let’s first install the Java SDK. Add a reference to the repository in pom.xml:

<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

Next, let’s add a reference to the dependency in pom.xml:

<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v4.25</version>
</dependency>
</dependencies>

Finally, let’s make our API call. Add the imports at the top of your file first, and then call the API:

// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.ValidateDocumentApi;
ApiClient defaultClient = Configuration.getDefaultApiClient();// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");
ValidateDocumentApi apiInstance = new ValidateDocumentApi();
File inputFile = new File("/path/to/inputfile"); // File | Input file to perform the operation on.
try {
DocumentValidationResult result = apiInstance.validateDocumentJsonValidation(inputFile);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling ValidateDocumentApi#validateDocumentJsonValidation");
e.printStackTrace();
}

Your parameters include your input file, and your Cloudmersive API key. If you don’t have a key, you can easily get one for free by registering a free account on our website. With your account, you’ll receive a limit of 800 API calls per month.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Cloudmersive

Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.