Can't send code output (array) to teams as JSON object

Describe the problem/error/question

Hi! I am trying to send adaptive cards formatting to a teams channel via HTTP request node. So I have a code node to format the adaptive cards. Although the code always outputs as an array instead of JSON object so the HTTP request will not send to teams. I've tried using stringify and editing my return value to not include the [] array. Any ideas on what I can do?

What is the error message (if any)?

Error 400 Bad Request. Missing body content. Expecting JSON object

Workflow Screenshot:

Here is the Javascript I have in the code node to format the adaptive cards

// n8n Code node
const text = items[0].json.output || items[0].json.text || ‘’;

/* (same parsing code as above) /
const lines = text.split(‘\n’);
const sections = [];
let currentSection = null;
for (let line of lines) {
line = line.trim();
if (!line) continue;
const match = line.match(/^(#+)\s+(.
)$/);
if (match) {
if (currentSection) sections.push(currentSection);
currentSection = { title: match[2].trim(), level: match[1].length, content: };
} else {
if (!currentSection) currentSection = { title: “Introduction”, level: 1, content: };
currentSection.content.push(line);
}
}
if (currentSection) sections.push(currentSection);

const cardBody = ;
const headingStyle = (level) => {
switch (level) {
case 1: return { size: “large”, weight: “bolder” };
case 2: return { size: “medium”, weight: “bolder” };
case 3: return { size: “default”, weight: “bolder” };
default: return { size: “default”, weight: “default” };
}
};

for (const section of sections) {
const container = { type: “Container”, items: };
if (section.title) {
const style = headingStyle(section.level);
container.items.push({
type: “TextBlock”,
text: section.title,
size: style.size,
weight: style.weight,
wrap: true,
spacing: “medium”
});
}
for (const para of section.content) {
container.items.push({ type: “TextBlock”, text: para, wrap: true, spacing: “small” });
}
cardBody.push(container);
}

const messagePayload = {
body: { contentType: “html”, content: “AI-Generated Security Strategy Summary” },
attachments: [
{
contentType: “application/vnd.microsoft.card.adaptive”,
content: {
$schema: “http://adaptivecards.io/schemas/adaptive-card.json”,
type: “AdaptiveCard”,
version: “1.2”,
body: cardBody
}
}
]
};

// n8n requires you return an array of items
return [
{
json: messagePayload
}
];

Share the output returned by the last node

[
{
“body”: {
“contentType”: “html”,
“content”: “AI-Generated Security Strategy Summary”
},
“attachments”: [
{
“contentType”: “application/vnd.microsoft.card.adaptive”,
“content”: {
“$schema”: “http://adaptivecards.io/schemas/adaptive-card.json”,
“type”: “AdaptiveCard”,
“version”: “1.2”,
“body”: [
{
“type”: “Container”,
“items”: [
{
“type”: “TextBlock”,
“text”: “Introduction”,
“size”: “large”,
“weight”: “bolder”,
“wrap”: true,
“spacing”: “medium”
},
{
“type”: “TextBlock”,
“text”: “Based on my research into Company’s database and understanding of Client’s requirements, here’s a comprehensive markdown document outlining their software supply chain security strategy:”,
“wrap”: true,
“spacing”: “small”
}
]
}
]
}
}
]
}
]

Information on your n8n setup

  • n8n version: [email protected]
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app): Running on chrome
  • Operating system:

Hey!
Unfortunately this is hard to debug, because your pasted workflow is not in a codeblock and thus not pasteable.

The output of the AI Agent is not robust. Although it works as intended most of the time, it may output a different structure too. Having a code node to format it is a good idea – yet even the Code node requires a stable input to work with. My suggestion is to use ‘Information Extractor’ node. It’s AI powered but it’s output is always a clean JSON structure.

I added this step. The information extractor is still outputting the information in an array. Therefore the HTTP request node cannot send the message to teams.

The response of AI Agent node is an array with objects, you just need to properly escape or format your output with an additional node (code).

This is what the Information Extractor returns – a proper JSON structure. However, all nodes in n8n return their output inside [Square Brackets] which makes it pass as an array. Even the Code node you set up to format the AI Agent response, will return an ‘array’ too.

My suggestion is to change the HTTP Request and use an alternative instead.

1 Like

Hi all, thanks for your responses. I decided to use a teams compatible HTML formatting instead of the adaptive cards. There doesn’t seem to be any real fix to my problem with removing array brackets from JSON code. So adaptive cards are not going to work at the moment. Hopefully this is fixed within n8n’s system soon.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.