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.

It posted the weather almost flawlessly for a few weeks, then stopped working.
Any ideas on how to fix this JSON error?
No one is using OpenAI node here?

First bot:

Second bot:

Hey @JabmoBabo,

Can you share the output from the node with the error and the content it is posting? Looking at the node data there are a bunch of folk using the OpenAI node so that is possibly unrelated.

This is Input JSON’s:

1 item
[
{
"id":
"chatcmpl-7P9XV6U1nfdnZ8oez7OU344OsogH",
"object":
"chat.completion",
"created":
1686229225,
"model":
"gpt-3.5-turbo-0301",
"usage":
{
"prompt_tokens":
132,
"completion_tokens":
269,
"total_tokens":
401
},
"choices":
[
{
"message":
{
"role":
"assistant",
"content":
"Cześć rowerowe kanapki! Oto Lily, Twoja ulubiona prezenterka pogody! Więc słuchajcie uważnie - jeśli dzisiaj planujecie jazdę na rowerze, to zdecydowanie polecam założyć ciepłe spodnie i kurtkę w kolorze wiatru (czytaj: szarym) - temperatury spadły do ,, a wiatr nie daje nam spokoju i osiąga prędkości km/h, które poruszają nie tylko Twoje drzewko. Brrr! Instalujcie swoje czapki i rękawiczki, żeby nie spadły z wiatrem! 🌪️ Mam nadzieję, że przetrwacie tę zimną jazdę i patrzcie na jaśniejszą stronę - w końcu przynajmniej nie będziecie się pocić! 💦 No cóż, wybaczcie mi, że musiałem dostarczyć złe wiadomości, ale zobaczymy się jutro o tej samej porze! 😂"
},
"finish_reason":
"stop",
"index":
0
}
]
}
]

Second one:

1 item
[
{
"id":
"chatcmpl-dfg345455v38v3k4FN5f4w54",
"object":
"chat.completion",
"created":
1686142805,
"model":
"gpt-3.5-turbo-0301",
"usage":
{
"prompt_tokens":
149,
"completion_tokens":
188,
"total_tokens":
337
},
"choices":
[
{
"message":
{
"role":
"assistant",
"content":
"Cześć wszystkim! Tutaj Lily z prognozą pogody. Dzisiaj o godzinie 15:00 jest pochmurnie, ale rowerzyści nie będą się martwić. Temperatura wynosi 21 stopni, więc lekkie koszulki i spodenki będą idealne. Wiatr jest lekki, więc warto zabrać kurtkę w razie zmiany warunków na drodze. Nie zapomnijcie o kasku i zabierzcie ze sobą butelkę wody! 💦 No i przede wszystkim, nie zapominajcie o uśmiechu na twarzy! 😁 Do zobaczenia jutro o tej samej porze! 🚴‍♀️🚴‍♂️☁️"
},
"finish_reason":
"stop",
"index":
0
}
]
}
]

Another:


[
{
"id":
"chatcmpl-45g34h567xaqmNZcKtg45g45",
"object":
"chat.completion",
"created":
1686198604,
"model":
"gpt-3.5-turbo-0301",
"usage":
{
"prompt_tokens":
146,
"completion_tokens":
345,
"total_tokens":
491
},
"choices":
[
{
"message":
{
"role":
"assistant",
"content":
""Witajcie Kochani! Oto Ashley - wasza ulubiona prezenterka pogody! Patrzę przez okno i...o matko, chyba zostaliśmy zapomniani przez słońce! Dziś jest pochmurnie i mało przyjemnie, ale nie dajcie się zniechęcić. Przygotujcie swoje rowery, bo wsiadamy na nie i jedziemy! Na pierwszy rzut oka temperatura wynosi 12.26 stopni Celsjusza, ale brrr...wydaje mi się, że jest o wiele chłodniej! Zdecydowanie polecam ciepłą bluzę, kurtkę, rękawiczki i oczywiście...czepek pod kask. Nie chcecie przecież złapać przeziębienia i kolejnego dnia spędzić w łóżku! A co z wiatrem? Na szczęście chłodzi nas on tylko trochę, dzisiaj przewidywana prędkość to 3.09 km/h, ale uważajcie na porywy! Mniejsza o to, jak się ubierzecie - ważne, żebyście ruszyli do przodu! A na zakończenie... 🤪 widzimy się jutro o tej samej porze! Trzymajcie się, moi śmiałkowie, i róbcie to, co lubicie najbardziej!""
},
"finish_reason":
"stop",
"index":
0
}
]
}
]

HTTP node JSON:

{"base64_attachments":["data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/4QBsRXhpZgAASUkqAAgAAAADADEBAgAHAAAAMgAAABICAwACAAAAAgACAGmHBAABAAAAOgAAAAA"], "message": "[Pogodynka] {{ $node.OpenAI1.json.choices[0].message.content }}" , "number": "+3123456789", "recipients": ["group.345645FzUWRVRkRNbFVCUDVkeVZSaG9kdW1td534534534UvaFh5bz0="]}     

But sometimes works:

Working JSON:

1 item
[
{
"id":
"chatcmpl-34r2345f243E2e4dLKIMpwerf34f",
"object":
"chat.completion",
"created":
1685939422,
"model":
"gpt-3.5-turbo-0301",
"usage":
{
"prompt_tokens":
129,
"completion_tokens":
229,
"total_tokens":
358
},
"choices":
[
{
"message":
{
"role":
"assistant",
"content":
"Cześć wszystkim słuchaczom! Oto najlepsza prezenterka pogody pod słońcem - Ashley! Dzisiaj jest godzina 7:30 rano, więc czas na małą poranną prognozę. No więc bierzcie rowery i jedziemy! Temperatura wynosi obecnie -5 stopni Celsjusza, z wiatrem wiejącym z prędkością 25 km/h. Radzę założyć spodnie, kurtkę, rękawiczki, czapkę i oczywiście tarcze na koła rowerowe. Zakładam, że wyglądacie już teraz jak prawdziwi mistrzowie ekstremalnych sportów zimowych! 😜 Więc jedziemy z pełną parą! Zobaczymy się jutro o tej samej porze, a ja już zaczynam się szykować do kolejnej wesołej prognozy! 😄"
},
"finish_reason":
"stop",
"index":
0
}
]
}
]

Another working from screenshot above:

1 item
[
{
"id":
"chatcmpl-234523trswVBEjQV17erfwgf345",
"object":
"chat.completion",
"created":
1685680202,
"model":
"gpt-3.5-turbo-0301",
"usage":
{
"prompt_tokens":
146,
"completion_tokens":
211,
"total_tokens":
357
},
"choices":
[
{
"message":
{
"role":
"assistant",
"content":
"🕡Dobrego poranka! Oto przewidywana pogoda o godzinie 06:30: duże zachmurzenie, 10.48 stopni celcjusza i 4.63 km/h wiatru 💨 Dla rowerzystów polecam dodać do swojego stroju ciepłą kurtkę i ochraniacze na uszy, aby uniknąć zamarznięcia i być przygotowanym na każdą okoliczność! ❄️ Ale nie martwcie się, bo przynajmniej będziecie mieli wiatr we włosach, kiedy będziecie pędzić w dół na rowerze 🚴‍♂️🚴‍♀️🌬️ Zobaczymy się jutro o tej samej porze z jeszcze więcej pozytywnych wieści! 😴👋"
},
"finish_reason":
"stop",
"index":
0
}
]
}
]

Today’s last node (Signal) input (output from OpenAI node):


[
{
"id":
"chatcmpl-7QTTcrpw0z546jjR5EgGg6iPzD43m",
"object":
"chat.completion",
"created":
1686544203,
"model":
"gpt-3.5-turbo-0301",
"usage":
{
"prompt_tokens":
146,
"completion_tokens":
198,
"total_tokens":
344
},
"choices":
[
{
"message":
{
"role":
"assistant",
"content":
""Dzień dobry wszystkim! No i mamy już 6:30 rano, cześć cześć! Słuchajcie, rowerzyści, jeśli macie ochotę przejechać się dzisiaj, to lepiej się cieplej ubierzcie. Mamy tu duże zachmurzenie, temperatura to tylko 17.17 stopni Celsjusza, a wiatr mało - tylko 2.06 km/h. Więc chłodno i wystarczy jedna warstwa ubrań, nie ma co przesadzać. Ale zawsze mogą Państwo założyć hełm na nadmiar włosów, to zawsze będzie ciepło! 😀 Na razie to tyle, do zobaczenia jutro o tej samej porze! 😉""
},
"finish_reason":
"stop",
"index":
0
}
]
}
]

Signal node JSON (deleted static base64 number and group id)

{"base64_attachments":["data:image/jpeg;base64,xxxxx"],"message": "[Pogodynka] {{ $node.OpenAI1.json.choices[0].message.content }}" , "number": "+31xxxxxxxxx", "recipients": ["group.Mdf4MHFzU234dsfFVC342VZ43dW1tdGdLM0hYM14fdsFh43f0="]}    

Error:

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)

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