Showing results for 
Search instead for 
Did you mean: 

Valid JSON is sent via AWS MQTT and recieved in PLC as an Invalid JSON structure

Valid JSON is sent via AWS MQTT and recieved in PLC as an Invalid JSON structure

New Contributor


We receive a JSON payload from an overhead system, every 15 minutes, via AWS MQTT. We receive this payload via the FB "AWS_IOT.AWSIoTSubscribe". Well, we noticed that several times a day, the payload coming in has one brace too many at the end resulting in invalid JSON structure and thus cannot be parsed. We can reproduce this error message. We have a payload that 100% of the time is sent as Valid JSON from AWS and comes in on the subscribe FB as Invalid JSON (1 curly brache to many at the end). And this while there are no additional process steps in the payload route.

We are using the CodeSyS IIOT libarary ( CORE version: 1.20, PLC App 1.20.3, CodeSyS version .

See attachment: the left text is recieved as string on our PLC side en the right text is being send by the AWS broker.

Kind regards


Community Moderator
Community Moderator

As I do not know the library, just a suggestion.
Could it be that the variable used as an output of the function block is not emptied by the application or the FB and so the new message is only overwriting a part of it? this could result in the additional curly bracket at the end if a number inside the string could be a digit shorter.


Hi @AndroidzZ ,

If you cannot find the source of the problem, you could correct for this by looping over the string and pushing each curly brace & bracket to seperate stacks. You then pop off the stack when you encounter an opposing brace or bracket. If you have any items remaining on either stack, you know you need to remove them from the end.

Community Moderator
Community Moderator

I addition to the 2 comments above you should do the following:

  • Ensure that the data which is sent is really correct, by using a different client, in a different environment. While I never used that interface, I can't give you a hint which client you can use. Maybe in the Amazon docu you can find some clients for testing.
    • Either you realize that the JSON is correct. => than it is a bug in the CoDeSys library
    • or you have the same error => than something on the sender side is not correct.

CoDeSys docu AWS IoT Core Client_en.pdf 


I suspect that the problem is in this corner. at the moment I receive a string. If I get a shorter string after this I still see remnants of the previous (longer) string. I have tried to empty the string with: <string> := '';
I've also tried the MemCpy function but the string still shows remnants of the previous longer string.

The problem seems clear now only finding a solution for it is not yet successful. What suggestions could I try?


Kind regards


Community Moderator
Community Moderator

Ok. Now this sounds like a string handling problem.

I expect that the function you are using is just copying the received characters, but not the end of string character. That's pretty common for many CoDeSys features. While a string needs a end of string (Ascii code =0) character after the last letter, you have to put this manually after the last letter. The easiest way is to use sysMemSet() from library SysMem. If you do not set this character, the string continues until the next random position of char(0).


  strJson: STRING(1000); // sizeOf(strJson) = 1000+1

// fill the complete string with Char(0)=End of string char
SysMemSet(pDest:=ADR(strJson), udiValue:=0, udiCount:=SIZEOF(strJson));
// insert here your code which copies the data into strJson
// e.g. receive JSON string



Established Member

Can you provide us with a copy of the string (as text) or an example?
In addition, which MQTT version is used here?
(Which MQTT parameters are used - Libman, MQTTParams)?

We are using AWS IoT Core Client SL library with MQTT_Clienst_SL

The text files in attachment are in structure identical but one with a character more than the other which will trigger the '{' problem 

Parameters we use are: