Persistent "JSON parameter needs to be valid JSON" Error in HTTP Request Node

Hi everyone,
I’m encountering a persistent issue with the HTTP Request node and I’m hoping someone can spot what I’m missing. The node fails with the error JSON parameter needs to be valid JSON right before execution, even though the input data appears to be a valid JavaScript object.
The Goal
I’m trying to query a generative AI API (Anthropic Claude). The workflow is:
A Code Node dynamically builds the request body object based on an incoming question.
An HTTP Request Node sends this object as a JSON payload to the API.
The Problem
The HTTP Request node consistently fails with this error:
JSON
{
“errorMessage”: “JSON parameter needs to be valid JSON”,
“errorDetails”: {},
“n8nDetails”: {
“nodeName”: “Claude Request”,
“nodeType”: “n8n-nodes-base.httpRequest”,
“nodeVersion”: 4.2,

}
}
Debugging Steps I’ve Already Taken
This is the strange part. We’ve isolated the issue extensively:
Initial Issue: My Code node was using JSON.stringify(). We thought it was a double-stringify issue, so we removed it to ensure the node outputs a raw JavaScript object. The error persisted.
Isolated the Data: We thought complex data from previous nodes might be interfering. So, we simplified the Code node to return only the necessary object for the API call. The output is now perfectly clean, but the error still persists.
Verified the Input: The output from the Code node (which is the input for the failing HTTP Request node) is confirmed to be a valid JavaScript object. Here is the exact output:
JSON
[
{
“original_question”: “how many clients does Karina have?”,
“claudeRequestBody”: {
“model”: “claude-3-5-sonnet-20240620”,
“max_tokens”: 4096,
“messages”: [
{
“role”: “user”,
“content”: “You are a helpful assistant for my data service. Interpret this query and respond ONLY with pure JSON, no explanations…\n\nQuestion: how many clients does Karina have?..”
}
]
}
}
]
The Key Test: We tested the HTTP Request node with a hardcoded/static JSON body. When we paste a simple JSON object directly into the “JSON” parameter field, the node works perfectly and the API call is successful.
Final Attempt: Knowing the hardcoded test worked, I deleted the expression in the “JSON” parameter field and carefully manually re-typed ={{ $json.claudeRequestBody }} to ensure there were no hidden characters or typos. The error returned immediately.
Node Configurations
Here is the setup that is failing.

  1. Code Node (Prepare AI Request)
    This node generates the clean output shown above.
    JavaScript
    const question = $input.item.json.question || “”;
    // The prompt is a long string instructing the AI to return JSON
    const prompt = `You are a helpful assistant for my data service. Interpret this query and respond ONLY with pure JSON, no explanations.
    Question: ${question}
    MAIN DATA MODELS:
  • data.contacts: (name, email, phone, is_company)
  • data.sales: (contact_id, total_amount, date, state)
  • data.tickets: (name, contact_id, stage_id, state, create_date)
    Respond only with the JSON:`;
    const claudeBody = {
    “model”: “claude-3-5-sonnet-20240620”,
    “max_tokens”: 4096,
    “messages”: [
    {
    “role”: “user”,
    “content”: prompt
    }
    ]
    };
    // Return a clean object with only the required data
    return {
    json: {
    original_question: question,
    claudeRequestBody: claudeBody
    }
    };
  1. HTTP Request Node (Claude Request)
    Method: POST
    URL: https://api.anthropic.com/v1/messages
    Authentication: Header Auth (Correct credentials selected)
    Headers:
    anthropic-version: 2023-06-01
    content-type: application/json
    Body Content Type: JSON
    JSON: ={{ $json.claudeRequestBody }} ← This seems to be the source of the failure.
    The core mystery is: Why does the node fail with the expression ={{ $json.claudeRequestBody }}, given that the input data ($json.claudeRequestBody) is a perfectly valid object, and the node itself works flawlessly when I paste a static object in the same field?
    I feel like I’m missing something very obvious. Any ideas would be greatly appreciated.
    Thanks in advance!

Hey @Lorena_Lopez hope all is good. Welcome back.

Try to:

  • remove the = before {}
  • jsonify the claudeRequestBody field:
{{ $json.claudeRequestBody.toJsonString() }}

if this won’t help…

Could you please share the workflow and pin the data for the previous node?

1 Like

Hi Jabbson!

That was it! Thank you so much, your first suggestion solved the issue immediately.

Switching to the legacy expression syntax by removing the leading = from the expression worked perfectly.

  • Working Expression: {{ $json.claudeRequestBody }}

  • Failed Expression: ={{ $json.claudeRequestBody }}

For anyone else who might find this thread, the problem seems to have been a subtle parsing issue with the V1 expression syntax (={{...}}) in this specific node’s “JSON” parameter. Even though the input data from the previous node was a perfectly valid JavaScript object, the V1 expression was causing the node’s internal validation to fail.

The key takeaway was that when a hardcoded body works but a V1 expression doesn’t, trying the legacy {{...}} syntax is a great debugging step.

Thanks again for the quick and accurate help. I was completely stuck on this!

1 Like

Glad I could help.

Kindly mark my answer as Solution and thank you.

Cheers!

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