JSON Parsing

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
enel_2129601
Level 3
Level 3
10 likes given 5 likes given First like received

Hello,

is there any document or any example related with JSON parsing. The JSON.c file is in the utilities.

Thanks

0 Likes
1 Solution
Anonymous
Not applicable

I just noticed your post for help. In response I put in a quick blog to help you understand how to read JSON files and extract tokens using ZentriOS. ZentriOS is designed to simplify many of the tasks in WICED by abstracting many of the functions into simpler APIs.

Take a look at this Beginners guide to ZentriOS : JSON Parser Example

Hope it helps!

View solution in original post

13 Replies
Anonymous
Not applicable

Hey,

You can modify the UART demo application to see how JSON parsing works.

In the uart.mk:- Paste the following :-

$(NAME)_COMPONENTS := utilities/cJSON

In uart.c include cJSON.h

#include "cJSON.h"

Copy the following function in uart.c

/* Parse text to JSON, then render back to text, and print! */

void doit(char *text)

{

    char *out;cJSON *json;

    json=cJSON_Parse(text);

    if (!json) {printf("Error before: [%s]\n",cJSON_GetErrorPtr());}

    else

    {

        out=cJSON_Print(json);

        cJSON_Delete(json);

       // printf("%s\n",out);

        WPRINT_APP_INFO(("%s",out));

        free(out);

    }

}

You can now call the doit(); function with JSON as the text from application_start();

Eg:   doit(text1);

Hope that helps.

Best Regards,

AB

0 Likes
enel_2129601
Level 3
Level 3
10 likes given 5 likes given First like received

Thank you for your kindly reply, but there is something that i do not understand that i could not find a cJSON.h file under utilities folder. I have JSON_parser so they are different or they are the same file? i am using SDK 3.5.2.

0 Likes
Anonymous
Not applicable

abirjepatil

I wish to know the answer to this as well.

0 Likes
Anonymous
Not applicable

cJSON refers to the open source library ...cJSON download | SourceForge.net

Make sure to put cJSON in libraries/utilities and create a new makefile which includes this source cJSON.c

You can then modify the application makefile to include this cJSON as a component. Finally include the cJSON.h in the app. 

Anonymous
Not applicable

You can also use jsmn.

See link: The most simple JSON parser in C for small systems

enel_2129601
Level 3
Level 3
10 likes given 5 likes given First like received

Thank you mkochhal

i have figured how to use JSON parser which is located in utilization folder in the real one which i asked.

i can explain in details for one who doesn't understand

Here is what i have done:

here we have a seperate parser_function depends on our JSON

static wiced_result_t parse_json_status(wiced_json_object_t * json_object )

{

     if(json_object->value_length > 0 && json_object->value_length < sizeof(port)-1)

          {

           memcpy(port, json_object->value, json_object->value_length);

          }

}

so in main part of code

wiced_JSON_parser_register_callback(parse_json_status);

then we are calling wiced_JSON_parser( argument )

after this when we call port we can reach the coming data from port variable.

Best wishes

Anonymous
Not applicable

Hi.

I just want to know how does the port variable store data? because if I have to do string operations to find type/payload/ etc, parsing JSON achieves nothing?

thanks!

0 Likes

i do not understant what do you mean so can you explain it more clearly?

Thanks

0 Likes
Anonymous
Not applicable

what if your JSON object looks like this:

{"messageType":"status",

  "serialNumber":"5724119a-d5d5-11e5-ab30-625662870761",

  "timeStamp":"2016-03-17T07:49:58.052496Z",

  "payload":{

       "deviceInfo":{"model":"2","firmwareVersion":"1.0.11.310"},

        "deviceStatus":{

             "usageStatus":1,

             "performance": {

                   "batteryPercent":100,

                   "batteryVolt":200,

                   "batteryAmp":11,

                   "batteryHealth":80,

                   "temperature":25,

                   "humidity":26,

                   "waterLevel":10,

                   "oxygen":120,

                    "co2":1,

                    "noise":100,

                    "leakage":36,

                    "vibration":5,

                     "pressure":60

             }

          },

   }

what will port contain?

0 Likes

i got it now. I am sorry i forgot to add a line of code segment so here the line

if(strncmp(json_object->object_string, "port", sizeof("port")-1) == 0)

     {

  //  WPRINT_APP_INFO(("lolll\n"));

         if(json_object->value_length > 0 && json_object->value_length < sizeof(port)-1)

         {

             memcpy(port, json_object->value, json_object->value_length);

           //  req_led_status[json_object->parent_object->value_length] = '\0';

         }

     }

so port is my variable and i check my json whether there is any port object at the end of tree. if so i take the value of it to my port variable. For your example it is "noise" or anything within the same branch with noise.

i hope it is more clear.

Best wishes

Anonymous
Not applicable

I just noticed your post for help. In response I put in a quick blog to help you understand how to read JSON files and extract tokens using ZentriOS. ZentriOS is designed to simplify many of the tasks in WICED by abstracting many of the functions into simpler APIs.

Take a look at this Beginners guide to ZentriOS : JSON Parser Example

Hope it helps!

enel_2129601
Level 3
Level 3
10 likes given 5 likes given First like received

Thank you for all your help it is more clear now

Anonymous
Not applicable

sure - anything we can do to help you turn that product into a reality!