Let AI triage your inbox for you

Step 5 of 13

05 / 13Code

Turn the reply into usable fields

Model output arrives as text, not structured data. Add a Code node to parse it, and make it survive a stray code fence rather than crashing the run.

The n8n panel for: Turn the reply into usable fields3 things to set here
  1. 1Open the Code nodedouble-click it on the canvas
  2. 2JavaScriptThe fallback means a malformed reply routes to needs_reply instead of failing the workflow.
  3. 3Check before moving onOutput shows clean category, urgency, summary, id and subject fields.
Code panel in n8nTap to enlarge
JavaScript
const raw = String($json.text ?? $json.output ?? '').replace(/```json|```/g, '').trim();
let parsed;
try { parsed = JSON.parse(raw); } catch { parsed = { category: 'needs_reply', urgency: 'normal', summary: 'Could not classify' }; }
return [{ json: { ...parsed, id: $('Gmail Trigger').item.json.id, subject: $('Gmail Trigger').item.json.subject } }];

The fallback means a malformed reply routes to needs_reply instead of failing the workflow.

Checkpoint
Output shows clean category, urgency, summary, id and subject fields.