Parsing JSON from webhook

Is there an easy way to pass a JSON string from a webhook to the ArduinoJson parser? In the following example, I get the error:

 invalid conversion from 'const char*' to 'char*' [-fpermissive]
void setup() {
    Particle.subscribe("hook-response/MBTA_test", condHandler, MY_DEVICES);

void condHandler(const char *event, const char *data) {
    JsonObject& root = jsonBuffer.parseObject(data);

The JSON parser needs to modify the incoming data so you can’t pass the data parameter since it’s marked const. So you need to create a copy which you can mess up as you like :wink:

void condHandler(const char *event, const char *data) {
  char copy[strlen(data)+1]; // depeinding on your JSON you may need some extra bytes
  strcpy(copy, data); 
  JsonObject& root = jsonBuffer.parseObject(copy);

On a related note, you could use Variable Substitution in the webhook to process out the parts of the JSON you need.


Thanks ScruffR that solved the problem! I will also look into variable substitution.

1 Like

Some care needs to be taken to ensure your copy doesn’t blow the stack away. I would suggest a static buffer of fixed length that you consider big enough to hold a typical string, with a safe copy function to ensure you don’t blow that away. Always beware of buffer overflows and safe coding practices.