JSON files are widely used to transmit data between different applications. CSV, on the other hand, is also a commonly used format to store the data in a comma separated format. In certain cases, you may need to import data from JSON files to CSV from within Node.js applications. For such cases, this article covers how to convert JSON data to CSV in Node.js.
- Node.js JSON to CSV Converter
- Steps to Convert JSON to CSV in Node.js
- Convert JSON Data or File to CSV
Node.js API for JSON to CSV Conversion
For JSON to CSV conversion, we will use Aspose.Cells for Node.js. It is a feature-rich API for creating, modifying, and converting spreadsheet files in Node.js applications. You can either download the API or install it using the following npm command.
npm install aspose.cells
Easy Steps to Convert JSON to CSV in Node.js
The following are the steps to save a JSON file in CSV format in a Node.js application.
- Create an Excel file.
- Select a specific sheet in the Excel file.
- Import data from JSON file to Excel sheet.
- Save Excel file in CSV format.
Let’s now transform the above-mentioned steps into Node.js code to convert a JSON file to CSV format.
Convert JSON File to CSV in Node.js
The following are the steps to convert JSON data to CSV format in Node.js.
- 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 converted CSV file after importing JSON data.
Node.js JSON to CSV Converter - Get a Free License
Get a temporary license and use Aspose.Cells for Node.js without evaluation limitations.
Conclusion
JSON and CSV files are widely used to store and transmit the data. In accordance with that, this article covered how to convert JSON data to CSV in Node.js applications. Furthermore, you can explore Aspose.Cells for Node.js using the documentation. Also, you can ask your questions via our forum.