Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting JSON string for AWS IoT

I'm getting "JSON format error" from the AWS console when I try to publish a temperature value from a variable; this works correctly:

mosquitto_pub -t \$aws/things/my-xxxx/shadow/update -m '{"state": {"desired":
{"temperature": 1   }}}' -q 1

I want to replace "1" with a variable so, I create a shell with the mosquitto_pub etc.., and I want to pass an argument to the shell, calling "./publish.sh Temperature_Value", where Temperature value is an int:

Trying this I get errors from AWS console:

DATA=${1}
mosquitto_pub -t \$aws/things/my-xxxx/shadow/update -m '{"state": {"desired":
{"temperature": $DATA }}}' -q 1

What am I doing wrong? Thanks

like image 340
DDBE Avatar asked Dec 05 '25 03:12

DDBE


1 Answers

Your escaping is wrong. this is the right escaping :

mosquitto_pub -t \$aws/things/my-xxxx/shadow/update -m "{\"state\": {\"desired\":
{\"temperature\": $1 }}}" -q 1

Remember that variables within single quotes ' are not interpolated.

Regards!

like image 184
Matias Barrios Avatar answered Dec 07 '25 18:12

Matias Barrios