/n8n-tutorials

How to handle escaped JSON from Cohere output in n8n?

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

Matt Graham, CEO of Rapid Developers

Book a call with an Expert

Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.

Book a free consultation

How to handle escaped JSON from Cohere output in n8n?

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.

 

Why this happens

 

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.

 

The reliable production-safe fix

 

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.

 

Extra notes that matter in real production flows

 

  • Always parse once. Never run JSON.parse multiple times on the same already‑parsed object, or you will get errors (“Unexpected token…”).
  • Validate first if the model sometimes returns non-JSON. You can wrap parse in a try/catch inside the same Function node to prevent executions from failing.
  • If Cohere output is in a different field (sometimes 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.
  • Keep the Function node close to the model node. That way, your entire workflow benefits from guaranteed clean structured data.
  • If you use error workflows: a malformed JSON from Cohere will throw inside JSON.parse. Catch it so you can alert or retry instead of silently breaking the run.

 

If Cohere returns JSON inside extra text

 

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.

Want to explore opportunities to work with us?

Connect with our team to unlock the full potential of no-code solutions with a no-commitment consultation!

Book a Free Consultation

Client trust and success are our top priorities

When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.

Rapid Dev was an exceptional project management organization and the best development collaborators I've had the pleasure of working with. They do complex work on extremely fast timelines and effectively manage the testing and pre-launch process to deliver the best possible product. I'm extremely impressed with their execution ability.

CPO, Praction - Arkady Sokolov

May 2, 2023

Working with Matt was comparable to having another co-founder on the team, but without the commitment or cost. He has a strategic mindset and willing to change the scope of the project in real time based on the needs of the client. A true strategic thought partner!

Co-Founder, Arc - Donald Muir

Dec 27, 2022

Rapid Dev are 10/10, excellent communicators - the best I've ever encountered in the tech dev space. They always go the extra mile, they genuinely care, they respond quickly, they're flexible, adaptable and their enthusiasm is amazing.

Co-CEO, Grantify - Mat Westergreen-Thorne

Oct 15, 2022

Rapid Dev is an excellent developer for no-code and low-code solutions.
We’ve had great success since launching the platform in November 2023. In a few months, we’ve gained over 1,000 new active users. We’ve also secured several dozen bookings on the platform and seen about 70% new user month-over-month growth since the launch.

Co-Founder, Church Real Estate Marketplace - Emmanuel Brown

May 1, 2024 

Matt’s dedication to executing our vision and his commitment to the project deadline were impressive. 
This was such a specific project, and Matt really delivered. We worked with a really fast turnaround, and he always delivered. The site was a perfect prop for us!

Production Manager, Media Production Company - Samantha Fekete

Sep 23, 2022