I am using libmosquitto and the parson library in a C application running on my IoT device for doing stuff with MQTT and parsing JSON.
When I publish a message to my device using the mosquitto_pub command on a Linux terminal as follows :
mosquitto_pub -t "mytopic/test" -u "admin" -P "admin" -h 192.168.5.100 -m "{"value1": 1, "value2": 2, "value3": 3}"
I am succesfully receiving the message on the device as I am subscribed to it, however, I cannot parse the values at all using json_object_dotget_value
JSON_Value* root_value = json_parse_string(payload);
JSON_Object* root_object = json_value_get_object(root_value);
JSON_Value* value1 = json_object_dotget_value(root_object, "value1");
JSON_Value* value2 = json_object_dotget_value(root_object, "value2");
JSON_Value* value3 = json_object_dotget_value(root_object, "value3");
The values returned are NULL.
I know that i'm leaving out lots of code here. However, the problem is not on the receiving end, because when another application in Python publishes this message, the parsing works fine.
There is something wrong with my mosquitto_pub
command, but I can't see it.
I'd appreciate any help. Thanks.
Per your comment, your program is fine.
In the invocation, you may have to escape the "
in the argument to your sender program (or use single quotes)
With -m "{"value1": 1, "value2": 2, "value3": 3}"
, the args [as the program sees them] are:
-m
{value1: 1, value2: 2, value3: 3}
Notice that the "
got stripped around (e.g.) value1
and json needs them.
So, change to -m '{"value1": 1, "value2": 2, "value3": 3}'
and the args become:
-m
{"value1": 1, "value2": 2, "value3": 3}