JSON parameter needs to be valid JSON on an HTTP request

Hey, I tried to call the Gemini 2.5 flash image model via HTTP request, but I’m getting this error that I can’t seem to resolve in the JSON code. I feel like I’m doing it right, and ChatGPT doesn’t see any errors either. If anyone knows how to fix it, I’m all ears :sweat_smile:

I also tried to use the “stringify” function in the code directly and in an external code node to change the format, but it’s not working either. :sob:

Thank you in advance

Change your "{{ $json.prompt}}"
to
"{{ $json.prompt.toJsonString() }}"

Without toJsonString(), the raw value may break JSON parsing (especially if it contains quotes, line breaks, or special characters).
Using .toJsonString() makes sure the value is escaped properly and produces valid JSON.

Otherwise you can use JSON.stringify() function, but you can use the .toJsonString() method also.

Hi @Armand_Ferveur

This is a common issue,

Could you please try to use JSON.stringify() like this:

"text": {{ JSON.stringify( $json.prompt) }}

:point_right: If this solves your problem, feel free to mark the reply as the solution :white_check_mark: it’ll help others with same issue, and it also helps me on the leaderboard :folded_hands:t2:

Doesn’t work either :sweat_smile: sry

Use mine :smiley:

same

can you share the output here?
before you running it

By the way, Why don’t you use the Gemini node, why you using HTTP node for that?

When using .toJsonString(), remove the quotation marks in the JSON body:

here is what it supposed to be:

{
  "contents": [
    {
      "parts": [
        {
          "text": {{ $json.prompt.toJsonString() }}
        }
      ]
    }
  ]
}

Otherwise you will end up with two quotation marks in the beginning and two at the end.

1 Like
{
  "contents": [
    {
      "parts": [
        {
          "text": "{{ $json.content.toJsonString() }}"
        }
      ]
    }
  ]
}

Use this, your JSON is wrong, since array cannot use key-value for that.
You need add one more object {} the curly branches which handle the text field

still not working :sweat_smile: I rly don’t know, i’ve allready done everything you’ve told me but it’s still doen’t want to work.

(btw, i didn’t use Gemini node because i didn’t knew it existed :joy: i’ll probably do that instead :rofl: )

it worked :saluting_face:

ALLELUIA MY FRIEND

Thx everyone :sparkling_heart:

1 Like

Hahahaa, yeah you can use the Gemini node by the way, easier for you to create AI things with the built in node:D

1 Like

Weird how .toJsonString() working and JSON.stringify() not in this case!
do you know the reason @jabbson ?

Both worked for me:

2 Likes

Yeah, I was going insane for a second lol :sweat_smile: Thanks, I can go now :grinning_face_with_smiling_eyes:

2 Likes

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