JSON error using code node after subworkflow execution

Describe the problem/error/question

After running a subworkflow I receive a json object. If i try to make a simple edit using node code, i get an error for the JSON output: Invalid output format [item 0] An output item contains the reserved key json. To get around this, please wrap each item in an object, under a key called json.
The only solution i found was to delete the metadata key from the item.
I believe this is not expected behaviour.

for (const item of $input.all()) {
delete item.metadata;
}
return $input.all();

Please share your workflow

Share the output returned by the last node

Invalid output format [item 0]
An output item contains the reserved key json. To get around this, please wrap each item in an object, under a key called json.

Information on your n8n setup

  • n8n version: 2.19.2
  • Database (default: SQLite): postgres
  • n8n EXECUTIONS_PROCESS setting (default: own, main): ?
  • Running n8n via (Docker, npm, n8n cloud, desktop app): docker
  • Operating system: ubuntu

Hey @jgggr, while you wait for a response, here are some things that might help:

Suggested resources

Automatically matched to your question.

Docs:

Forum:

@David_Streuli, @Matze202 - you’ve helped with similar issues before, can you take a look?

Automatically suggested by n8n’s community bot. It’s a pilot - please share feedback here.

Hi @jgggr
The Execute Sub-workflow node adds a top-level metadata key to every item it returns, which is what the “View sub-execution” link uses, and the Code node only allows json, binary, pairedItem and error as top-level keys on an output item, so return $input.all() gets rejected. Build the output items instead of returning the input ones:

return $input.all().map((item, i) => {
  const json = { ...item.json };
  if (json.agent_name === "example") json.agent_name = "other example";
  return { json, pairedItem: i };
});

pairedItem keeps item linking working for the nodes after it.

This was reported upstream and closed as stale, so building the items is the way around it for now:

Thanks for letting us know about this, We have created NODE-5788 as the internal dev ticket to look into it.

Thank you for your reply.

The solution workaround you describe I believe is the same thing as i’ve suggested (to delete the metadata key) right?

The GitHub issue you mentioned is spot on the problem I’ve described.

Same idea, and for the items that actually get cleaned the outcome is identical. The version in the workflow JSON you pasted isn’t equivalent though: delete item.metadata sits inside the case "example": branch, so only items whose agent_name matches get cleaned and every other item still carries metadata and trips the same error. Building the output item covers all of them without a branch, and it keeps working if the node ever returns another top-level key alongside metadata.