jsonlarge-language-modeln8n

How to parse LLM results from text to Json in n8n?


I ask an LLM node something along the lines of

I want you to tell me a joke and return it as json: {"question1":, "answer1":, "question2":, "punchline":

It returns something like:

```json
{
    "question1": "Why don't programmers like nature?",
    "answer1": "It has too many bugs.",
    "question2": "Why do Java developers wear glasses?",
    "punchline": "Because they don't C#."
}
```

which is a string and not a Json.

I am trying many types of parsing, but JSON.parse does not seem to be able to eat this up.

For example

{{$json["message"]["content"].replace("\`\`\`json", '').replace("\`\`\`", "")}}

creates a valid json, which then

{{JSON.parse($json["message"]["content"].replace("\`\`\`json", '').replace("\`\`\`", ""))}}

returns undefined on


How to parse text into json from an LLM in n8n?


Solution

  • enter image description here

    Took me a while to find this little switch. I couldn't make it happen with code, but looks like n8n got me covered. What a waste of time.