JSON formatting in nRF connect SDK 1.8.0 on nRF52840

I was wondering if there was a JSON formatter that could take a string and convert it to proper JSON.  I checked the cJSON library and it seems to only be supported through nRF connect SDK 1.2.0, and it also didn't have any functions for formatting the JSON.  Is there anything available to do this?

This may be outside of Nordic's scope as it is an amazon web service issue but I'm trying to pass correctly formatted JSON to an AWS endpoint.  Currently I'm just using snprintk and formatting myself:

snprintk(testJSON, 75, "{\"Start_Time\": \"%s\", \"End_Time\": \"%s\"}", time_pairs[0].startTime, time_pairs[0].endTime);

on AWS. However on AWS I get the error 'Message cannot be displayed in specified format' even though the message received on Amazon {"Start_Time": "10:44:56 2/3/22", "End_Time": "11:4:1 2/3/22"} passes JSON validators.


I was wondering if there was something wrong or if there was a specific function needed to avoid this error.

Thanks.

  • Hi, 

    Maybe you can do something like this:

    cJSON *root;
    root = cJSON_CreateObject();
    cJSON_AddNumberToObject(root, "Start_Time", time_pairs[0].startTime); // or you can cJSON_AddStringToObject
    cJSON_AddNumberToObject(root, "End_Time", time_pairs[0].endTime);
    // here is the JSON format you are looking for
    char *json_out = cJSON_Print(root);

    // Free memory
    cJSON_Delete(root);
    cJSON_free(json_out);
Related