N8n respond to webhook with JSON giving invalid JSON error despite evaluation showing correct details

Multiple issues with Respond to webhook , JSON. Sometimes although , the response body evaluates the expression successfully ( i can see it in results window), the output still throws an Invalid JSON in the response body


Describe the problem/error/question

What is the error message (if any)?

Please share your workflow

(Select the nodes on your canvas and use the keyboard shortcuts CMD+C/CTRL+C and CMD+V/CTRL+V to copy and paste the workflow.)

Share the output returned by the last node

Information on your n8n setup

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

Your json has new lines which makes it invalid.

{
  "success": true,
  "title": "Comprehensive Eyecare Routine for Screen Users",
  "prompt": "Create a comprehensive, step-by-step daily and weekly eyecare routine tailored for individuals who spend extended hours in front of digital screens. The guide should specifically include:

1. Eye exercises to reduce eye strain and improve focus flexibility.
2. Proper care instructions for bi-weekly disposable contact lenses.
3. Best practices for cleaning and maintaining reading glasses.
4. Screen ergonomics: Ideal settings for brightness, contrast, and blue light filters.
5. Workspace lighting recommendations to minimize glare and eye fatigue.
6. Recommended screen time management strategies and rest schedules (e.g., 20-20-20 rule).
7. Nutrition and hydration tips to support overall eye health.

Organize the routine clearly into daily and weekly actions, with an emphasis on prevention and long-term ocular wellness.",
  "refinement_type": "clarity",
  "user_feedback": "Focus on eye exercises, lens care, especially bi-weekly disposable lenses, reading glasses care, screen time etc"
}

When you use this json in expression, add .toJsonString(), which will take care of your problem.

Here is an example for your json with and without using .toJsonString(), see how it works.

Hope it helps.!

Thank you so much !!!

Kindly mark the answer as solution if it was helpful, thank you!

Cheers!