I’m struggling with something in n8n. I need to get data from the Code node, but instead of having it formatted as an array (i.e., without square brackets), I need it to output in a plain JSON format so I can use it directly in the HTTP node.
Every time I try to retrieve the data, it seems to wrap it in brackets, treating it as an array. Is there a specific way to format or extract data from the Code node that bypasses this, so I can keep it as plain JSON for easier use in the next steps?
Any advice or tips would be really appreciated. Thanks!
Welcome to the community! n8n works as an ETL tool digesting data as you see fit. Data passes through nodes as an array of objects (you can read more about this and why here: Understanding the data structure | n8n Docs)
That said, the http node will iterate over the items of the array provided by the code node. Can you share a bit more detail on what you intend to do here? Do you need to send a POST message with a JSON response from the code node? If so, you can return the json as a value from a key and build your HTTP response from it, something like this:
Hello @gualter!
I apologise in advance for the crooked explanation, I’m not an expert in coding unfortunately(
I’ve got a chain going:
Google sheet (get all the links that are in the column)
Code:
It has certain data to be passed on to the http node, which will send the data to Apify.
Here’s the part I need to send to Apify:
{
aggressivePrune: true,
clickElementsCssSelector: “.jobTitle > a, .job-card-container__link”,
crawlerType: “playwright:adaptive”,
debugLog: false,
debugMode: false,
dynamicContentWaitSecs: 10,
expandIframes: true,
htmlTransformer: “readableText”,
ignoreCanonicalUrl: false,
initialConcurrency: 5,
keepUrlFragments: false,
maxConcurrency: 10,
maxCrawlDepth: 1,
maxRequestRetries: 5,
maxResults: 5000,
maxScrollHeightPixels: 5000,
maxSessionRotations: 10,
minFileDownloadSpeedKBps: 128,
proxyConfiguration: {
useApifyProxy: true,
apifyProxyGroups: [“BUYPROXIES94952”]
},
readableTextCharThreshold: 100,
removeCookieWarnings: true,
requestTimeoutSecs: 60,
saveFiles: false,
saveHtml: false,
saveHtmlAsFile: false,
saveMarkdown: true,
saveScreenshots: false,
startUrls: urls,
useSitemaps: false
};
Noda Code takes all URLs from google sheet and pastes them into startUrls like this: (screenshot)
There goes an undetermined number of links, as it will be different each time. This time, there are 300 links.
The problem is that the Code node outputs data in , which I can’t pass to the http request, it says it’s an array.
if you want to send the startUrls array of URLs and they have that structure (assuming you always get 1 item from the previous node) you can just pass them like so in your config JSON:
startUrls: $input.all()[0].json.startUrls,
and no need to iterate over the previous input like that.
If you have multiple items in the previous array you can combine their startUrls into 1 and iterate over that using the agregate node.
Let me know if that helps. If you have more than 1 item, how do you intend to perform the HTTP calls?