I am trying to build a JSON object in n8n that combines multiple text chunks into a single string and then passes it along with a voiceId for text-to-speech.
However, I keep running into issues with how n8n handles mustache expressions inside JSON.
If I put ={{ $json.paragraphs.map(...) }} the JSON breaks.
Essentially, I can’t figure out the correct way to mix valid JSON formatting with the n8n mustache syntax so that my final output is a proper JSON object that contains both the concatenated text and the voiceId.
What I’m trying to achieve
I want the final JSON to look like this:
I’m trying to send a JSON body to an API in n8n using the HTTP Request node. The JSON looks like this:
{
"text": "Introduction:\nBlack holes are regions of space where gravity is so intense that nothing, not even light, can escape. They are among the most extreme objects in the universe and reveal how gravity shapes space and time.\n\nBody:\nMost black holes form when massive stars exhaust their nuclear fuel and collapse under their own gravity. The collapsing core compresses mass into a very small region, creating an event horizon, which is the boundary beyond which information cannot return. At the center lies what general relativity calls a singularity, where density and curvature become extreme and our current theories break down.\nBlack holes range from a few times the Sun's mass to supermassive giants at the centers of galaxies with millions or billions of solar masses. Matter falling toward a black hole forms an accretion disk that heats up and emits X-rays and, in some cases, powerful jets. Strong gravity bends light, producing gravitational lensing, and can stretch objects into long shapes in a process commonly called spaghettification.\nQuantum effects introduce additional complexity. Theory predicts a faint form of thermal emission known as Hawking radiation, which causes black holes to lose mass extremely slowly. We detect black holes not by seeing them directly but by observing their influence: energetic radiation from accreting gas, the motion of nearby stars, and gravitational waves from merging black holes.\n\nClosing:\nBlack holes are not cosmic vacuum cleaners but compact objects governed by general relativity and quantum puzzles that remain unsolved. They serve as powerful probes of fundamental physics. Each new observation brings us closer to understanding how gravity, matter, and spacetime behave under the most extreme conditions.",
"voiceId": "en-US-natalie"
}
When I test this payload in Postman, it works perfectly.
But when I try the same payload in n8n’s HTTP Request node, it throws an error saying the JSON format is invalid.
I’ve already:
Tried using RAW JSON mode in the node.
Escaped newlines with \\n instead of \n.
Verified Content-Type: application/json.
Still, n8n rejects it, but Postman accepts it.
It seems n8n is stricter about JSON escaping, or I might be structuring the body incorrectly.
Thank you very much, I have solved the problem, Turns out the issue was not the text body being in a JSON parameter. The issue was the header and authorization. I put the authorization to none and then specified the details header details with json and it worked.