JSON files are widely used to store and transmit structured data between applications. On the other hand, CSV is also utilized to store the data, such as datasets, as comma-separated values. In this article, you will learn how to export data from JSON files to CSV programmatically. More specifically, the article will cover how to convert JSON data to CSV using Java.
Java API for JSON to CSV Conversion
Aspose.Cells for Java is a powerful API to create and manipulate spreadsheet documents from within the Java applications. We will use this API to convert the JSON data to CSV format. You can either download Aspose.Cells for Java or install it using the following Maven configurations.
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>https://repository.aspose.com/repo/</url>
</repository>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-cells</artifactId>
<version>21.9</version>
</dependency>
Convert a JSON Data or File to CSV
The following are the steps to convert JSON data to CSV format using Java.
- Create a new Workbook object.
- Get reference of the first worksheet using Workbook.getWorksheets().get(0) method.
- Create an object of JsonLayoutOptions class to set additional options.
- Import data from JSON to CSV using JsonUtility.importData() method.
- Save the CSV file using Workbook.save(string, SaveFormat.CSV) method.
The following code sample shows how to convert JSON data to CSV format.
JSON Data
The following is the JSON data that we used in the above code sample.
[{"nodeId":1,"reputation":1134},{"nodeId":2,"reputation":547},{"nodeId":3,"reputation":1703},{"nodeId":4,"reputation":-199},{"nodeId":5,"reputation":-306},{"nodeId":6,"reputation":-49},{"nodeId":7,"reputation":1527},{"nodeId":8,"reputation":1223}]'
Converted CSV
The following is the screenshot of the data converted to CSV format.
Get a Free License
You can get a temporary license and use Aspose.Cells for Java without evaluation limitations.
Conclusion
JSON and CSV files are immensely used to keep and exchange data. Often, you need to export data from JSON files to CSV programmatically. For such cases, this article covered how to convert JSON data or files to CSV using Java. Alongside, you can also explore Aspose.Cells for Java using the documentation. In addition, you can ask your questions via our forum.