Namaste Community Member,
My development environment comprises of
- FreeRTOS version: V202002.00. (@file iot_demo_mqtt.c)
- FreeRTOS Serializer V1.1.1 (@file iot_json_utils.c)
Requirement
- Publish a JSON formatted payload from the AWS IoT Console (Publish to a subscribed topic).
- Obtain the JSON Value corresponding to a specific Key.
Issue Description
For the above second step, I am leveraging the already available JSON Serializer library available at FreeRTOS\libraries\c_sdk\standard\serializer\src\iot_json_utils.c
Within the _mqttSubscriptionCallback() API in the @file iot_demo_mqtt.c, I am calling the IotJsonUtils_FindJsonValue() API. For my testing purpose, I am passing a hardcoded “time” string as the Key to obtain the associated Value, which is populated incorrectly.
Publish JSON Message
Below is the simple JSON formatted message I am publishing from the AWS IoT Console.
{
"time":123456,
"action":1
}
Firmware Code Block
static void _mqttSubscriptionCallback( void * param1,
IotMqttCallbackParam_t * const pPublish )
{
....
....
bool retVal = false;
const char * value = NULL;
size_t lengthOfValue = 0;
size_t lengthOfTimeKey = strlen("time");
retVal = IotJsonUtils_FindJsonValue( pPayload,
(size_t) pPublish->u.message.info.payloadLength,
"time",
lengthOfTimeKey,
&value,
&lengthOfValue );
if( false == retVal )
{
IotLogError( "IotJsonUtils_FindJsonValue() failed." );
}
else
{
IotLogInfo( "length of pPayload = %d", pPublish->u.message.info.payloadLength );
IotLogInfo( "length of \"time\" string = %d", lengthOfTimeKey );
IotLogInfo( "value = %d", value );
IotLogInfo( "lengthOfValue = %d", lengthOfValue );
}
}
Console Logs
Below is the console log viewed on the USB Serial port.
The variable lengthOfValue is correctly populated as 6. However, the value itself is incorrectly populated as 1073566202 or a similar number instead of the expected number 123456.
116 6553 [iot_thread] [INFO ][DEMO][65530] Incoming PUBLISH received:
Subscription topic filter: dualOutlet/topic/RFID
Publish QoS: 0
Publish payload: {
"time":123456,
"action":1
}
118 6553 [iot_thread] [INFO ][DEMO][65530] length of pPayload = 33
119 6553 [iot_thread] [INFO ][DEMO][65530] length of "time" string = 4
121 6554 [iot_thread] [INFO ][DEMO][65540] value = 1073566202
122 6554 [iot_thread] [INFO ][DEMO][65540] lengthOfValue = 6
Requesting assistance to obtain the correct value for the given key of my JSON payload.
Thanks | Regards,
Dipen