I have an API return that I would like to export as a CSV file. The trick I am running into is that I only want to grab the area inside the content array and then return that data. What steps can I take to get that working? Is there a specific plugin that can help there?
Do you want to download the csv file from the browser or do everything behind the scenes from the user? I asked ChatGPT how to create a CSV file and the following Javascript will work. You will just need to use the free Toolbox plugin to get the action Run Javascript and then you can paste the following code after the API call:
This first section is the only part that you will have to insert dynamic data.
// Example text data
const textData = "Result of Step1's content:format as text";
Then in the format as text box make use \n as the delimeter and for each item say “This content’s id, This content’s Name, This content’s state”
// Convert text data into an array
const dataArray = textData.trim().split('\n').map(row => row.split(','));
// Define headers for CSV file
const headers = ['id', 'Name', 'state'];
// Function to convert array to CSV string
function arrayToCSV(data) {
const csvRows = [];
csvRows.push(headers.join(','));
data.forEach(row => {
csvRows.push(row.join(','));
});
return csvRows.join('\n');
}
// Convert array to CSV string
const csvData = arrayToCSV(dataArray);
// Create Blob object for CSV data
const blob = new Blob([csvData], { type: 'text/csv' });
// Create download link for CSV file
const downloadLink = document.createElement('a');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'data.csv';
downloadLink.click();