Need a little help to make my Code node to process multiple items

Describe the problem/error/question

Unable to process multiple output in Code node despite choosing “Run once for each item”

What is the error message (if any)?

ERROR: Unknown top-level item key: companyName [item 0]
Access the properties of an item under .json, e.g. item.json

Please share your workflow

// Get the input data

let inputData = $items('Set')[0].json.companyName;
let processedData = inputData.replace(/\s/g, '').toLowerCase();

return {"companyName": processedData};

(Select the nodes on your canvas and use the keyboard shortcuts CMD+C/CTRL+C and CMD+V/CTRL+V to copy and paste the workflow.)

Share the output returned by the last node

Input Data - {
“companyName”: “Macwel leadtech LLC”
Output Data must have:

  • Remove URL
  • Convert all characters to lowercase
  • Remove symbols
  • Remove common naming conventions (remove : LLC/Corp/Inc)
  • Remove leading and trailing whitespace
  • Capitalize the first letter of each word

It would be great if someone can help me update the code.

Information on your n8n setup

  • **n8n version:**Latest (cloud)
  • **Database (default: SQLite):**No DB
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app): Cloud
  • Operating system:

You may want to try
let inputData = $('Set').item.json.companyName; or just $input.item.json.companyName; if it’s coming as output from the previous node. Hope that works anyway

I tried let inputData = $('Set').item.json.companyName; still the same error but work if “run once for all items”.

I would like to process multiple items through this node which is not the case :grimacing:

Hi @Ajayen_yogakumar - welcome to the community :tada:

Sorry to hear you’re having trouble with this - following up from @jonflow 's reply, the “run once for each” option is quite strict in the data it expects. It’ll be wanting data in this format:

  json: {
    // Your data

You can find more information about that here.

A full code example that might work for you is here in this sample workflow:

Let me know if that helps!

1 Like

Hi @EmeraldHerald thank you for the response. I get it, I will have to construct a Json payload to execute multiple items. But where I am stuck exactly is, I am pushing multiple values from a column in Gsheet as Input value. In this case my I cannot construct a Json payload since I may not know how many items I will be processing in a single execution. Attaching my workflow screen shot for reference.

Anticipating your response.

Hey @Ajayen_yogakumar,

Did you check the example? That should do what you are after.

Hi @Jon @EmeraldHerald @jonflow I tried and made it work. So the updated script can process multiple items now that will “run once for all items”.I appreciate your quick support on this :pray: :handshake:

Here is the modified script that I used;

// Get the input data
let inputData = $items('Set2') // change this to the proper set to pull input value
let outputData = []

for (let i = 0; i < inputData.length; i += 1) {
  let inputDataItem = inputData[i].json.location;
  // split the string by comma and get the first word
  let processedData = inputDataItem.split(',')[0].trim();
  // remove extra whitespaces and non-alphabetic characters
  processedData = processedData.replace(/[^a-zA-Z ]/g, '').trim();

  let outputDataItem = {}
  outputDataItem["location"] = processedData;

return outputData;
1 Like

Hey @Ajayen_yogakumar,

If the Set2 node is the node being used before the Code node you could also use the loop example we load the node with so you would end up with something like this…

let outputData = [];
for (const item of $input.all()) {
  outputData.push( {
    'location': item.json.location.split(',')[0].replace(/[^a-zA-Z ]/g, '').trim()

return outputData;

Ignore the location setting… The for loop is the important bit there so you can see how you could use it.

Thank you, I will try it