Learn how to handle escaped JSON from Cohere in n8n with simple steps to clean, parse, and reliably use AI-generated data.

Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
A Cohere model often returns JSON as a string instead of actual JSON. In n8n that means your next node receives something like "{"title":"Hello","tags":["a","b"]}" instead of a real object.
The fix is: run the escaped string through a Function node and use JSON.parse() to turn it into a proper JavaScript object. After that, every downstream node will receive clean, usable JSON.
Cohere (like OpenAI) sometimes wraps JSON output in quotes, escapes every quote inside, and sends it back as plain text. n8n does not automatically “unescape” that. So your “JSON” is actually a text string containing backslashes. Downstream nodes (like Set, HTTP Request, or Item Lists) can’t work with that unless you convert it.
Use a Function node immediately after the Cohere node:
// n8n Function node
// Assumes the Cohere response text is in items[0].json.text
// Adjust the field name if your Cohere node returns something else
return items.map(item => {
const raw = item.json.text; // this is the escaped string from Cohere
const parsed = JSON.parse(raw); // turn it into a real JS object
return {
json: {
...item.json,
parsedJson: parsed // you now have clean JSON here
}
};
});
After this Function node, use {{$json["parsedJson"]}} in expressions. It will behave like normal JSON with no escaping issues.
item.json.generations[0].text), change the path accordingly. You can inspect the exact field by using an Execute Node or clicking the output preview of the Cohere node.JSON.parse. Catch it so you can alert or retry instead of silently breaking the run.
Sometimes Cohere wraps JSON in explanations or markdown fences. Then you first need to extract the JSON substring before parsing:
// n8n Function node with JSON extraction
return items.map(item => {
const raw = item.json.text;
// Extract JSON between first { and last }
const jsonString = raw.substring(raw.indexOf("{"), raw.lastIndexOf("}") + 1);
const parsed = JSON.parse(jsonString);
return {
json: {
parsedJson: parsed
}
};
});
This is the stable, production-safe way to handle escaped JSON from Cohere inside n8n.
When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.