Learn how to parse and handle escaped JSON from Cohere API output in n8n using JSON Parse and Function nodes for seamless workflow integration.
Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
To handle escaped JSON from Cohere output in n8n, you need to parse the escaped string into a usable JSON object. This typically involves using the JSON Parse node with appropriate configuration to process the escaped characters in the Cohere API response.
Step 1: Understand the Cohere Output Problem
When working with the Cohere API in n8n, you might receive responses where JSON is escaped within a string. This means the actual JSON structure is represented as a text string with escape characters (like " or \n), making it difficult to access the nested properties directly.
For example, instead of getting a proper JSON object like:
{
"generations": [
{
"text": "Some response text",
"data": { "key": "value" }
}
]
}
You might receive an escaped string representation like:
{
"response": "{"generations":[{"text":"Some response text","data":{"key":"value"}}]}"
}
Step 2: Create Your Workflow with Cohere Node
First, set up your workflow with a trigger node and the Cohere node:
Step 3: Examine the Cohere Response
After running the Cohere node, examine the output to identify the escaped JSON:
Step 4: Add a JSON Parse Node
To handle the escaped JSON, add a JSON Parse node:
[Cohere Node] → [JSON Parse Node]
Step 5: Configure the JSON Parse Node
Configure the JSON Parse node to handle the escaped JSON:
data.response
or json.generations
{
"parameters": {
"mode": "property",
"property": "json.response", // Adjust this path based on your Cohere output
"options": {
"keepOnlyParsed": true
}
}
}
Step 6: Handle Deeply Nested Escaped JSON
Sometimes, the escaped JSON might be deeply nested or appear in an array. In such cases:
// Add this Function node after the Cohere node
const cohere\_response = items[0].json; // Adjust according to your data structure
// Find the escaped JSON string (path may vary)
const escapedJson = cohere\_response.response; // Or another path where your escaped JSON is
// Parse the escaped JSON into an actual object
const parsedData = JSON.parse(escapedJson);
// Return the parsed data
return [{
json: parsedData
}];
Step 7: Access the Parsed JSON Data
After parsing, you can now access the properties directly:
{{ $node["JSON Parse"].json.generations[0].text }}
Step 8: Handle Arrays of Escaped JSON
If you receive an array of items with escaped JSON, use a "Split In Batches" node followed by a "JSON Parse" node in a loop:
Step 9: Error Handling
Add error handling to manage parsing failures:
// Condition for IF node
{{ $node["JSON Parse"].json !== undefined }}
Step 10: Test and Verify
Finally, test your workflow to ensure the escaped JSON is correctly parsed:
Alternative Solution Using Function Node Only
If the JSON Parse node doesn't work for your specific case, use a dedicated Function node:
// Complete solution using Function node
function processCohere(items) {
try {
const cohereResponse = items[0].json;
// Identify where the escaped JSON is in your response
// This path will vary depending on your Cohere API call
const escapedJsonPath = cohereResponse.response ||
cohereResponse.generations ||
cohereResponse.result;
// If it's a string, parse it
let parsedData;
if (typeof escapedJsonPath === 'string') {
parsedData = JSON.parse(escapedJsonPath);
} else {
// If it's already an object, use it directly
parsedData = escapedJsonPath;
}
// Return the clean, parsed data
return [{
json: {
original: cohereResponse,
parsed: parsedData
}
}];
} catch (error) {
// Handle any parsing errors
return [{
json: {
error: true,
message: `Failed to parse Cohere response: ${error.message}`,
original: items[0].json
}
}];
}
}
return processCohere(items);
By following these steps, you'll be able to properly handle and work with escaped JSON from Cohere API responses in your n8n workflows, making the data accessible and usable for subsequent operations.
When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.