{"product_id":"flatten-nested-json-arrays-in-n8n-with-code-node","title":"Flatten Nested JSON Arrays in n8n with Code Node","description":"\u003ch3\u003eTurn nested JSON into clean lead items in n8n\u003c\/h3\u003e\n\u003cp\u003eIf your API response includes a nested \u003cstrong\u003eleads array\u003c\/strong\u003e, this n8n workflow helps you \u003cstrong\u003eflatten it into one item per lead\u003c\/strong\u003e—while keeping parent metadata and converting each lead’s \u003cstrong\u003einterests\u003c\/strong\u003e into a ready-to-use CSV string.\u003c\/p\u003e\n\n\u003ch3\u003eWhat this workflow does\u003c\/h3\u003e\n\u003cul\u003e\n  \u003cli\u003e\n\u003cstrong\u003eManual start:\u003c\/strong\u003e The workflow begins when you run it manually.\u003c\/li\u003e\n  \u003cli\u003e\n\u003cstrong\u003eCreates sample nested JSON:\u003c\/strong\u003e It generates a sample payload that includes a \u003cstrong\u003esource\u003c\/strong\u003e field and a \u003cstrong\u003eleads\u003c\/strong\u003e array of lead objects.\u003c\/li\u003e\n  \u003cli\u003e\n\u003cstrong\u003eSplits leads into individual items:\u003c\/strong\u003e Using the flattening logic, it breaks the nested \u003ccode\u003epayload.leads\u003c\/code\u003e array into separate n8n items—one per lead.\u003c\/li\u003e\n  \u003cli\u003e\n\u003cstrong\u003eAdds parent context:\u003c\/strong\u003e Each resulting item is enriched with parent metadata (such as the payload’s \u003cstrong\u003esource\u003c\/strong\u003e) and the lead’s `array index`.\u003c\/li\u003e\n  \u003cli\u003e\n\u003cstrong\u003eFormats interests as CSV:\u003c\/strong\u003e It also builds an \u003cstrong\u003einterests CSV\u003c\/strong\u003e string for easier downstream processing (for example, for mapping, exporting, or storage).\u003c\/li\u003e\n  \u003cli\u003e\n\u003cstrong\u003eSummarizes output:\u003c\/strong\u003e At the end, it reports how many lead items were produced and returns an example output item.\u003c\/li\u003e\n\u003c\/ul\u003e\n\n\u003ch3\u003eUse cases\u003c\/h3\u003e\n\u003cul\u003e\n  \u003cli\u003ePrepare flattened lead data for \u003cstrong\u003edatabase upserts\u003c\/strong\u003e or CRM ingestion.\u003c\/li\u003e\n  \u003cli\u003eNormalize API responses so you can reliably \u003cstrong\u003eexport leads to CSV\u003c\/strong\u003e or feed them into reporting steps.\u003c\/li\u003e\n  \u003cli\u003eMake automation nodes downstream simpler by converting nested arrays into \u003cstrong\u003eone-item-per-record\u003c\/strong\u003e structures.\u003c\/li\u003e\n  \u003cli\u003eTest and iterate on array-path changes (for example, if your nested array isn’t at \u003ccode\u003epayload.leads\u003c\/code\u003e).\u003c\/li\u003e\n\u003c\/ul\u003e\n\n\u003ch3\u003eTechnical details\u003c\/h3\u003e\n\u003cul\u003e\n  \u003cli\u003e\n\u003cstrong\u003eNodes used:\u003c\/strong\u003e Code node (core flattening\/formatting), Sticky Note (workflow documentation), Manual Trigger.\u003c\/li\u003e\n  \u003cli\u003e\n\u003cstrong\u003eSetup:\u003c\/strong\u003e Replace the sample data step with your real JSON source (e.g., Webhook or HTTP Request), update the array path (currently \u003ccode\u003epayload.leads\u003c\/code\u003e), and adjust carried-over fields to match your payload structure.\u003c\/li\u003e\n\u003c\/ul\u003e\n\n\u003cp\u003e\u003cstrong\u003eBest for:\u003c\/strong\u003e n8n users, automation engineers, and SaaS operators who need reliable \u003cstrong\u003enested JSON array flattening\u003c\/strong\u003e in the Code node.\u003c\/p\u003e","brand":"N8N Commerce","offers":[{"title":"Default Title","offer_id":46075755102387,"sku":"N8N-19824","price":17.99,"currency_code":"GBP","in_stock":true}],"thumbnail_url":"\/\/cdn.shopify.com\/s\/files\/1\/0749\/6279\/6723\/files\/f_Pd-xT9SDU-S739vkkzl_VexCLX5b.png?v=1790154343","url":"https:\/\/buyflowscripts.com\/products\/flatten-nested-json-arrays-in-n8n-with-code-node","provider":"N8N Commerce","version":"1.0","type":"link"}