DDBE
DDBE

Reputation: 345

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

Upvotes: 3

Views: 449

Answers (1)

Matias Barrios
Matias Barrios

Reputation: 5056

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!

Upvotes: 5

Related Questions