Parsing with SparkJSON

I am trying to use a JSON string as a control string, so I can pass a bunch of arguments as a group (rather than with multiple cloud function calls).

The argument would look like this, and I am using a Particle.function() call:

{"opMode":1, "brightness":500, "period":2500,"msg":"ABC123"}

My code for the function:

int processControl(String command){
    
    char conString[200];
    command.toCharArray(conString, sizeof(conString));
    StaticJsonBuffer<200> jsonBuffer;
    JsonObject& root = jsonBuffer.parseObject(conString);
 
  
    if(root["brightness"]>0) {
        Brightness = root["brightness"];
    }
    if(root["opMode"]>0) {
        OperationMode=root["opMode"]; 
    }
    if(root["period"]>0) {
        loopPeriod=root["period"];
    }
    
    return 0;
}

I copied this verbatim from code that I wrote 2-3 years ago that worked. Now it’s not working at all.

  1. Why is this not working?

  2. I am using the IFs to check if the values are >0 to validate whether or not the parameters are even in the JSON. Is there a way to know whether or not they were even included? That is, I’d like to be able to send zero as an argument, or ignore that setting if it’s absent.

  3. I am having ZERO luck setting root[“stringValue”] to a String type.

thanks!

Can you post your entire code or even post a SHARE THIS REVISION link if you are using Web IDE.


Although I haven’t heard back from you I went ahead and tested your code and for me this works
https://go.particle.io/shared_apps/5c7823ceab7ac200050500b3

1 Like