i want you to write javascript code for a code node in n8n, such that: json\n{\n "Caption": "Obsidian iceberg meets katana! 🔪 #obsidianart #knifeskills #satisfyingcuts #oddlysatisfying #fyp #foryou #viral #trending #asmr #oddlysatisfyingvideo #satisfying #tiktok",\n "Idea": "Black obsidian iceberg gets sliced cleanly with a katana.",\n "Environment": "Close-up on a reflective black surface. Precise katana cuts create small obsidian shards scattering in slow motion. Minimalist, dark studio setting.",\n "Sound": "Sharp, clean slicing sound followed by the faint tinkle of obsidian shards.",\n "Status": "for production"\n}\n this garbage output is cleaned in various input fields for the next successive node. note that the fields i want are: id, idea, caption, production, environment_prompt, sound_prompt, final_output
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Of course. Here is the JavaScript code for an n8n Code node that will clean and transform your input data into the desired format.
You can copy and paste this directly into the JavaScript code editor of the Code node in your n8n workflow.
JavaScript Code for n8n Code Node
javascript
// This code will process each item passed from the previous node.
// It restructures the data, cleans it, and creates new fields.
const newItems = [];
for (const item of items) {
// Get the input JSON data for the current item.
const inputData = item.json;
// Create a new object to hold our cleaned-up output data.
const outputData = {};
// 1. Generate a unique ID.
// We'll create a simple one using the current timestamp and a random string.
outputData.id = `vid_${Date.now()}_${Math.random().toString(36).substring(2, 9)}`;
// 2. Extract and clean the "Idea" field.
// We use .trim() to remove any leading/trailing whitespace.
// The '|| ""' ensures we get an empty string if the field doesn't exist.
outputData.idea = (inputData.Idea || "").trim();
// 3. Extract and clean the "Caption" field.
outputData.caption = (inputData.Caption || "").trim();
// 4. Determine the "production" status.
// This creates a boolean (true/false) value. It's true only if the "Status"
// field exists and is exactly "for production" (case-insensitive).
outputData.production = (inputData.Status && inputData.Status.toLowerCase() === 'for production');
// 5. Extract and clean the "Environment" prompt.
outputData.environment_prompt = (inputData.Environment || "").trim();
// 6. Extract and clean the "Sound" prompt.
outputData.sound_prompt = (inputData.Sound || "").trim();
// 7. Create a "final_output" by combining the core creative prompts.
// This is useful for feeding into a single text field in a subsequent node.
const finalOutputParts = [
outputData.idea,
outputData.environment_prompt,
outputData.sound_prompt
];
// We filter out any empty parts and join the rest with a space.
outputData.final_output = finalOutputParts.filter(part => part).join(' ');
// Add the newly structured JSON object to our results array.
// n8n requires each item in the returned array to have a 'json' property.
newItems.push({
json: outputData
});
}
// Return the array of new, transformed items to the next node.
return newItems;
How It Works
Looping Through Items: The code starts by looping through items, which is the standard n8n variable holding all the data passed from the previous node.
Accessing Data: For each item, it accesses the data via item.json.
Creating New Fields:
id: A unique ID is generated using the current time and a random string. This is useful for tracking or as a primary key.
idea & caption: It takes the original Idea and Caption fields, converts them to lowercase field names (idea, caption), and uses .trim() to remove any accidental spaces at the beginning or end. The || "" is a safety measure to prevent errors if the input field is missing.
production: This is a smart boolean. It checks if the Status field exists and if its value (converted to lowercase) is "for production". The result will be true or false.
environment_prompt & sound_prompt: Similar to idea, these fields are extracted from Environment and Sound and cleaned.
final_output: It intelligently combines the idea, environment_prompt, and sound_prompt into a single, space-separated string. This is perfect for feeding into a generative AI or API that requires a single consolidated prompt. It also filters out any empty parts to avoid extra spaces.
Returning Data: Finally, it returns a new array (newItems) where each object has a json property containing your cleanly structured data, ready for the next node.
Example Transformation
If this is the input to the Code node:
json
{
"Caption": "Obsidian iceberg meets katana! 🔪 #obsidianart #knifeskills #satisfyingcuts #oddlysatisfying #fyp #foryou #viral #trending #asmr #oddlysatisfyingvideo #satisfying #tiktok",
"Idea": "Black obsidian iceberg gets sliced cleanly with a katana.",
"Environment": "Close-up on a reflective black surface. Precise katana cuts create small obsidian shards scattering in slow motion. Minimalist, dark studio setting.",
"Sound": "Sharp, clean slicing sound followed by the faint tinkle of obsidian shards.",
"Status": "for production"
}
The Code node will produce this output for the next node:(Note: The id will be different each time the code runs)
json
{
"id": "vid_1678886400000_a1b2c3d4e",
"idea": "Black obsidian iceberg gets sliced cleanly with a katana.",
"caption": "Obsidian iceberg meets katana! 🔪 #obsidianart #knifeskills #satisfyingcuts #oddlysatisfying #fyp #foryou #viral #trending #asmr #oddlysatisfyingvideo #satisfying #tiktok",
"production": true,
"environment_prompt": "Close-up on a reflective black surface. Precise katana cuts create small obsidian shards scattering in slow motion. Minimalist, dark studio setting.",
"sound_prompt": "Sharp, clean slicing sound followed by the faint tinkle of obsidian shards.",
"final_output": "Black obsidian iceberg gets sliced cleanly with a katana. Close-up on a reflective black surface. Precise katana cuts create small obsidian shards scattering in slow motion. Minimalist, dark studio setting. Sharp, clean slicing sound followed by the faint tinkle of obsidian shards."
}