How to get around the famous bug: "A valid JSON must be provided"?

I looked through the first 5 google pages about this problem, talked to gpt yesterday for a few hours, no solution ever. So it is simply a bug.

Is there any way to “clean up” the code in another node?

I tried to get around the bug by using database Baserow in between and first save the text there, and retrieve it, but still once in a few times of bot responses, n8n recognizes that JSON is still invalid.

All the data below is from the same request.

ERROR: JSON parameter need to be an valid JSON details:

NodeOperationError: JSON parameter need to be an valid JSON
    at Object.execute (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/HttpRequest/V3/HttpRequestV3.node.js:1070:35)
    at Workflow.runNode (/usr/local/lib/node_modules/n8n/node_modules/n8n-workflow/dist/Workflow.js:652:51)
    at /usr/local/lib/node_modules/n8n/node_modules/n8n-core/dist/WorkflowExecute.js:596:68
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

JSON that is causing the problem. According to the validators, it has the correct format:


[
{
"id":
1,
"order":
"1.00000000000000000000",
"Wiadomosc":
"wymień listę 10 komedii lat 80",
"Odpowiedz":
"The Breakfast Club 1985 Ferris Buellers Day Off 1986 Ghostbusters 1984 Airplane 1980 The Princess Bride 1987 Trading Places 1983 National Lampoons Vacation 1983 Caddyshack 1980 The Naked Gun From the Files of Police Squad 1988 This Is Spinal Tap 1984"
}
]

The whole core of communication of the upper weather branch with the last Signal node has been tested for several months and works without problems.
After adding the OpenAI node, Signal sometimes fails to parse the code and returns a JSON error.
If anyone knows how to effectively call a “check message” request, I’d be happy to learn.


BTW

With this I have the same problem, every few answers the same error appears. So it seems to me that the problem may be with the OpenAI output.

Usually,

can be resolved by JSON.stringify()-ing the incoming data in the parameter input.

1 Like

Hey, thanks for the reply.

The whole workflow works for simple responses, the bot normally communicates through Signal (with or without code node). But when I write “list 10 movies of the 80s” a JSON error appears and the message is not delivered.

I applied such code on the node preceding the output and the same error appears:

const inputData = JSON.stringify(items[0].json);
const outputData = inputData;

return {
  json: {
    outputData: outputData
  }
};

In the next node:

const inputData = JSON.parse(items[0].json.outputData);
const outputData = inputData.message.content;

return {
  json: {
    outputData: outputData
  }
};

My JSON which is “incorrect” according to n8n:

[

{

"outputData":"Oto 10 losowych filmów z lat 80: 1. "Krzyk" (1984) 2. "Czas apokalipsy" (1986) 3. "Indiana Jones i Świątynia Zagłady" (1984) 4. "Top Gun" (1986) 5. "Powrót do przyszłości" (1985) 6. "Pluton" (1986) 7. "Gremliny" (1984) 8. "Omen III: Ostatnie Starcie" (1981) 9. "Scarface" (1983) 10. "Poltergeist" (1982)"

}

]

Hey @JabmoBabo,

That json looks invalid to me you have " in your data and they are not escaped which will cause issues.

Are you able to actually share your workflow json so we can take a proper look?

Previously, using JavaScript code written by GPT, I replaced the characters " with spaces, and removed some “whitespace characters” and it did not help at all.

So currently, every 10s Schedule node trigger HTTP Request to Signald container, checking if there are new messages available. (I do not know how to trigger this type of workflow in a different way).

IF node, it forwards the entire message if it contains /gpt characters.

Next Code node extracts only the “message” part from the received string and removes the /gpt characters.

OpenAI node, gets the extracted clean message and completes the task.

The next Code block, is a suggested JSON to string conversion, which creates a string with “content” in the middle.

The last Code block extracts the “content” from the string and passes it to the HTTP Request.

The HTTP Request, sends a message through the Signald API to the Signal messenger.

The problem with JSON occurs behind the OpenAI node, I guess in every case.
It is worth adding that this workflow works and the error appears once every 5 messages. Usually when some lists of things appear I guess.

Hey @JabmoBabo,

If it is only on certain runs it will be the data, the example you provided looked invalid so I suspect it is more data like that.

I would make sure you escape special characters and that will probably solve all of the issues.

What special characters are not allowed, or how to “clear” the data so that the error does not appear?

Hey @JabmoBabo,

In theory all characters allowed but if you are starting your string with " it will expect the next " to be the end so you would need to need for these to make sure it is valid json.

1 Like

Using Code node I replaced the " character with a space and this did not help.

Hey @JabmoBabo,

You won’t want to replace all of them. Have you ran your output through a json linter to see if it shows as being valid?

1 Like

Suppose I have an Input node, before the last node that sends everything to Signal.
If this Input node contains, for example, clear text, weather, etc, there is no problem and Signal delivers all the messages.

If I replace the Input node with an OpenAI node, then once in several responses, the Signal node returns an error.

If I insert a Code node between the OpenAI node and Signal node with code that removes the characters " from the response, it still does not help.
This does not affect the message structure in Signal node.