How to Extract JSON from a Basic Chain LLM Node Response in n8n

Hi n8n community,
I’m building a custom job-hunting workflow and I need some help. I’m using the Basic Chain LLM node, and its response comes as a string inside a JSON array. Here’s a simplified example of the response I get:
[
{
“text”: “{\n "Rating": 10,\n "Rating Explanation": "The candidate’s resume perfectly matches the job description, covering all necessary skills and experience with exceptional depth.",\n "Cover Letter": "Dear Hiring Manager,\n\nAs an experienced … of this role and explore how I can make valuable contributions to the organization. Thank you for considering my application. Sincerely,\n Jasser H"”
}
]
I want to extract this JSON from the string so I can work with its fields (like Rating, Rating Explanation, and Cover Letter) in subsequent workflow nodes.
Does anyone have a clean approach or example on how to parse this string into proper JSON in n8n?
Thanks in advance!

To extract and parse the JSON string from the LLM node’s response in n8n, you can use a Code node to convert the string inside the “text” field into a usable JSON object. This is a common scenario when LLMs return a JSON as a string rather than as a parsed object.

Here’s a clean approach, based on official n8n community guidance:

**Example Code Node Implementation:**

// Get the string from the “text” field

const text = $input.first().json.text;

// Parse the string into a JSON object

const parsed = JSON.parse(text);

// Return the parsed object so its fields are accessible in later nodes

return [{ json: parsed }];

**How to use:**

1. Place this Code node right after your Basic LLM Chain node.

2. The output of this node will have the fields (e.g., Rating, Rating Explanation, Cover Letter) as top-level keys, making them easy to use in subsequent nodes.

**Why this works:**

This approach is recommended when the LLM returns a JSON as a string, which is a common pattern. Parsing it in a Code node ensures you can access each field directly in your workflow.

If your string contains problematic characters (like unescaped newlines), you may need to pre-process the string before parsing. For example, you can use a regular expression to replace problematic .

const cleaned = text.replace(/\n(?=[^“]*”(?:[^“]*”[^“]*”)*[^"]*$)/g, ‘\\n’);

const parsed = JSON.parse(cleaned);

return [{ json: parsed }];

This helps avoid parsing errors due to special characters inside the stringified JSON.

**References:**

- [n8n Community: How to fix and parse broken JSON lines from webhook input?]

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