I'm calling Anthropic API /v1/messages with HTTP Request node. The messages field is an array built in a Code node. When I use jsonBody with {{ $json.messages }} I get "Unexpected end of JSON input". How do I correctly send an array field?

I’m calling Anthropic API /v1/messages with HTTP Request node. The messages field is an array built in a Code node. When I use jsonBody with {{ $json.messages }} I get “Unexpected end of JSON input”. How do I correctly send an array field?

Hi @guido_veliz Welcome!
Have you tried using {{ $json.messages.toJsonString() }} without any quotes?

welcome to the n8n community @guido_veliz

What I usually do in n8n is build the entire request body as a single expression to make sure it’s valid JSON. For example, I’d set it up like this: {{ { “model”: “claude-sonnet-4-20250514”, “max_tokens”: 1024, “messages”: $json.messages } }} Anthropic expects messages as an array field inside the full payload, so building the whole body this way is usually my safest bet.

Both approaches above will work. Just to add one more option for anyone searching later: if you prefer to keep things in the “JSON” body mode instead of Raw/Custom, you can also use $json.messages.toJsonString() in the value field. That method converts the array to a valid JSON string inline.

But honestly, switching to Raw/Custom body and using JSON.stringify() for the whole payload (as Benjamin suggested) is what I do in production - it’s cleaner and avoids any surprise serialization issues down the line, especially when your messages array starts getting more complex with system prompts or multi-turn history.