Skip to content

Instantly share code, notes, and snippets.

@marcio-azevedo
Last active May 20, 2026 12:06
Show Gist options
  • Select an option

  • Save marcio-azevedo/b2f751bb68ad001bf648d7cb6aff7820 to your computer and use it in GitHub Desktop.

Select an option

Save marcio-azevedo/b2f751bb68ad001bf648d7cb6aff7820 to your computer and use it in GitHub Desktop.
JSON Parser into CSV - AI Adoption & Ops Velocity - Kick Off
// Initialize the CSV with the header row
let csvOutput = "Quote,Author\n";
// Loop through all incoming items passed natively by n8n
for (const item of $input.all()) {
// Safely navigate the JSON structure
const content = item.json.content || {};
const parts = content.parts || [];
for (const part of parts) {
const text = part.text || "";
if (text.includes("—")) {
// Split the quote and author by the em-dash
const segments = text.split("—");
let quotePart = segments[0];
let authorPart = segments[1];
// Clean up surrounding spaces, newlines, and outer quotes
let quote = quotePart.trim().replace(/^"|"$/g, '');
let author = authorPart.trim();
// Handle internal quotes for valid CSV formatting (double them up)
quote = quote.replace(/"/g, '""');
author = author.replace(/"/g, '""');
// Append the row to our output string
csvOutput += `"${quote}","${author}"\n`;
}
}
}
// Return the final CSV string inside the structure n8n expects
return {
csv_result: csvOutput.trim()
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment