13 Replies Latest reply on Mar 18, 2016 1:21 AM by engin.875

    JSON Parsing

    engin.875

      Hello,

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

      Thanks

        • 1. Re: JSON Parsing
          abirjepatil

          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

          • 2. Re: JSON Parsing
            engin.875

            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.

            • 3. Re: JSON Parsing
              mkochhal

              abirjepatil

              I wish to know the answer to this as well.

              • 4. Re: JSON Parsing
                mkochhal

                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. 

                1 of 1 people found this helpful
                • 5. Re: JSON Parsing
                  mkochhal

                  You can also use jsmn.

                   

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

                  1 of 1 people found this helpful
                  • 6. Re: JSON Parsing
                    engin.875

                    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

                    1 of 1 people found this helpful
                    • 7. Re: JSON Parsing
                      ndutton

                      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!

                      2 of 2 people found this helpful
                      • 8. Re: JSON Parsing
                        engin.875

                        Thank you for all your help it is more clear now

                        1 of 1 people found this helpful
                        • 9. Re: JSON Parsing
                          ndutton

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

                          1 of 1 people found this helpful
                          • 10. Re: JSON Parsing
                            mkochhal

                            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!

                            • 11. Re: JSON Parsing
                              engin.875

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

                              Thanks

                              • 12. Re: JSON Parsing
                                mkochhal

                                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?

                                • 13. Re: JSON Parsing
                                  engin.875

                                  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

                                  1 of 1 people found this helpful