N8n MongoDB Node AI agent: JSON object inserted as string instead of fields

Hi everyone,

I’m working on a workflow in n8n using the n8n AI Agent to collect user info (last name, first name, email, and phone number) via WhatsApp, and then store that data in a MongoDB collection.

The agent receives the user’s answers through conversation, then sends the data to a MongoDB Insert Tool. The problem is:
Instead of inserting each field (lastName, firstName, email, phone) correctly, the node stores everything as a single key with null values.

As you can see below in the image, the whole object is inserted as a single key (stringified JSON), not as a real document with 4 fields.


My questions:
How can I make sure the agent outputs a real JSON object, not a stringified version?
Is there a way to parse or convert the agent’s output in n8n before it goes into MongoDB?

Thank you for your help.

Have you tried using a code node to standardize this output? You can ask an AI to generate a JavaScript code that takes this output and converts it to the format you want. I usually do this when it comes to sending data to a database.

The problem is that I’m doing it in a agent and when I use a code node, it needs a string as an output because the LLM need a string apparently to read the data. So I could not return an objectif for mongodb to read it.

Nevertheless I have tried this and it works just fine

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