Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@groupdocs-cloud-gists
Last active November 21, 2022 22:47
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/d8f1ef0c2e2e1337737be2f680c35be4 to your computer and use it in GitHub Desktop.
Save groupdocs-cloud-gists/d8f1ef0c2e2e1337737be2f680c35be4 to your computer and use it in GitHub Desktop.
Convert CSV to JSON and JSON to CSV in Java
package com.groupdocsdev.classes;
import com.groupdocs.cloud.conversion.api.*;
import com.groupdocs.cloud.conversion.model.*;
import com.groupdocs.cloud.conversion.model.requests.*;
import com.groupdocs.cloud.conversion.client.Configuration;
import com.groupdocs.cloud.conversion.client.ApiException;
import java.util.List;
// How to Convert JSON to CSV File in Java using REST API.
public class App {
public static void main(String[] args) {
// Create an instance of the convert API
ConvertApi apiInstance = new ConvertApi(configuration);
try {
// Prepare convert settings
ConvertSettings settings = new ConvertSettings();
settings.setStorageName(MyStorage);
settings.setFilePath("java-testing/input-sample-file.json");
settings.setFormat("csv");
settings.setOutputPath("java-testing/output-sample-file.csv");
// convert to specified format
List<StoredConvertedResult> response = apiInstance.convertDocument(new ConvertDocumentRequest(settings));
System.out.println("Document converted successfully: " + response);
} catch (ApiException e) {
System.err.println("Exception while calling Java API: ");
e.printStackTrace();
}
}
}
package com.groupdocsdev.classes;
import com.groupdocs.cloud.conversion.api.*;
import com.groupdocs.cloud.conversion.model.*;
import com.groupdocs.cloud.conversion.model.requests.*;
import com.groupdocs.cloud.conversion.client.Configuration;
import com.groupdocs.cloud.conversion.client.ApiException;
import java.util.List;
// How to Convert CSV to JSON in Java using REST API.
public class App {
public static void main(String[] args) {
// Create an instance of the convert API
ConvertApi apiInstance = new ConvertApi(configuration);
try {
// Prepare convert settings
ConvertSettings settings = new ConvertSettings();
settings.setStorageName(MyStorage);
settings.setFilePath("java-testing/input-sample-file.csv");
settings.setFormat("json");
settings.setOutputPath("java-testing/output-sample-file.json");
// convert to specified format
List<StoredConvertedResult> response = apiInstance.convertDocument(new ConvertDocumentRequest(settings));
System.out.println("Document converted successfully: " + response);
} catch (ApiException e) {
System.err.println("Exception while calling Java API: ");
e.printStackTrace();
}
}
}
package com.groupdocsdev.classes;
import com.groupdocs.cloud.conversion.api.*;
import com.groupdocs.cloud.conversion.model.requests.*;
import com.groupdocs.cloud.conversion.client.Configuration;
import com.groupdocs.cloud.conversion.client.ApiException;
import java.io.File;
// Download File from the Cloud Storage in Java
public class App {
public static void main(String[] args) {
// Create an instance of the convert API
FileApi apiInstance = new FileApi(configuration);
try {
DownloadFileRequest request = new DownloadFileRequest("java-testing\\output-sample-file.json", MyStorage, null);
File response = apiInstance.downloadFile(request);
System.err.println("Expected response type is File: " + response.length());
} catch (ApiException e) {
System.err.println("Exception while calling FileApi:");
e.printStackTrace();
}
}
}

You can transform CSV to JSON and JSON to CSV online programmatically on the cloud. In this article, you will learn how to convert CSV to JSON and JSON to CSV in Java.

The following topics are covered in this article:

  1. Java CSV to JSON and JSON to CSV Converter API - Installation
  2. How to Convert CSV to JSON in Java using REST API
  3. How to Convert JSON to CSV File in Java using REST API

Read the complete article on how to convert CSV to JSON and JSON to CSV in Java: https://blog.groupdocs.cloud/2022/11/21/convert-csv-to-json-and-json-to-csv-in-java/

package com.groupdocsdev.classes;
import com.groupdocs.cloud.conversion.api.*;
import com.groupdocs.cloud.conversion.model.*;
import com.groupdocs.cloud.conversion.model.requests.*;
import com.groupdocs.cloud.conversion.client.Configuration;
import com.groupdocs.cloud.conversion.client.ApiException;
import java.io.File;
// Upload File to Cloud Storage using Java
public class App {
public static void main(String[] args) {
// Create an instance of the convert API
FileApi apiInstance = new FileApi(configuration);
try {
File fileStream = new File("H:\\groupdocs-cloud-data\\input-sample-file.csv");
UploadFileRequest request = new UploadFileRequest("java-testing\\input-sample-file.csv", fileStream, MyStorage);
FilesUploadResult response = apiInstance.uploadFile(request);
System.out.println("Expected response type is FilesUploadResult: " + response.getUploaded().size());
} catch (ApiException e) {
System.err.println("Exception while calling FileApi:");
e.printStackTrace();
}
}
}
# Get your client_id and client_secret from https://dashboard.groupdocs.cloud after free registration.
String ClientId = "xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
String ClientSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
String MyStorage = "test-internal-storage";
Configuration configuration = new Configuration(ClientId, ClientSecret);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment