Parsing JSON help

I’m reading in from Firebase via a webhook. It appears I’m getting the hook response with the firebase read… but I can’t parse it back into number variable formats. Any help would be appreciated.

// This #include statement was automatically added by the Particle IDE.
#include <SparkJson.h>
#include "Particle.h"

//Parameter Variables
int     Cycle = 20.0;
double  LReadPgm;
double  LReadWeb;
double  LWritten;
int     ReadParametersInterval = 15; // #Freqnency to poll web for new parameters
String  deviceName;
const char *CHECK_EVENT_NAME = "ParametersRead";

void setup()
{
    Particle.subscribe("spark/", handler);
    Particle.publish("spark/device/name");
    Particle.subscribe("hook-response/ParametersRead", getDataHandler, MY_DEVICES);
}

void loop() 
{
    ReadParameters();
}

void ReadParameters()
{
    if((Time.now() - LReadWeb) >= ReadParametersInterval)
    {
        char pREAD[255];
        snprintf(pREAD, sizeof(pREAD), "{\"n\":\"%s\"}",deviceName.c_str());
        Particle.publish(CHECK_EVENT_NAME, pREAD);
        LReadWeb = Time.now();
    }
}

void handler(const char *topic, const char *data) 
{
    deviceName = String(data);
    //Particle.publish("Device Name: " + String(deviceName), String(deviceName));
}

void getDataHandler(const char *topic, const char *data) 
{
    StaticJsonBuffer<256> jsonBuffer;
    char *mutableCopy = strdup(data);
    JsonObject& root = jsonBuffer.parseObject(mutableCopy);
    free(mutableCopy);
    //Particle.publish("ParametersREAD: ", String(data), PRIVATE);

    // All data, including numbers, are represented as strings, so we need to convert them back to their native data type here
    int newCycle =         atoi(root["Cycle"]);
    double newLReadPgm =   atof(root["LReadPgm"]);
    double newLReadWeb =   atof(root["LReadWeb"]);
    double newLWritten =   atof(root["LWritten"]);
    Particle.publish("New Params:::", (String(newCycle) + "|" + String(newLReadPgm) + "|" + String(newLReadWeb) + "|" + String(newLWritten)), PRIVATE);
    delay(3000);
}


How about printing out the parsed strings before converting them via atoi()/atof()?
You should also not have a delay(3000) in a subscribe handler.
I’d also rather use String::format() instead of all these String() concatenations.

ok, still stuck at the parser… I modifed the code to try to print the parsed strings, but can’t seem to get passed successfully on the parser. I’m copying the data variable into mutableCopy, that works, I can publish it back out, but when I pass it through the json parser it fails my success test.

void getDataHandler(const char *topic, const char *data) 
{
    char *mutableCopy = strdup(data);
    Particle.publish("The mutableCopy:", String(mutableCopy));
    JsonObject& root = jsonBuffer.parseObject(mutableCopy);
    if(!root.success())
    {
        Particle.publish("DEBUG", "ParseObject() failed");
        Particle.publish("The AFTER-PARSING mutableCopy:", String(mutableCopy));
        return;
    }
    const char* NewCycle = root["Cycle"];
    const char* NewLReadPgm = root["LReadPgm"];
    
    Particle.publish("Parm ---->>>>> READ:", String(NewCycle));
}

Can you Serial.print the strings and post that output instead of a screenshot that doesn’t show the whole string?
As it seems your JSON isn’t valid for some reason and that can only be seen when showing the whole string.

yes, 3 loops of serial.println here i’m printing data right after coming into the function… and then mutableCopy right before the parse and then mutableCopy after the parse

void getDataHandler(const char *topic, const char *data) 
{
    StaticJsonBuffer<256> jsonBuffer;
    Serial.println(data);
    char *mutableCopy = strdup(data);
    Serial.println(mutableCopy);
    JsonObject& root = jsonBuffer.parseObject(mutableCopy);
    Serial.println(mutableCopy);
    if(!root.success())
    {
        Particle.publish("DEBUG", "ParseObject() failed");
        Particle.publish("The AFTER-PARSING mutableCopy:", String(mutableCopy));
        return;
    }
    const char* NewCycle = root["Cycle"];
    const char* NewLReadPgm = root["LReadPgm"];
    
    Particle.publish("Parm ---->>>>> READ:", String(NewCycle));
}
{"Cycle":"20","LReadPgm":"1486232986","LReadWeb":"1486233856","LWritten":"1486233844","PB":"60","PMode":"2","PToggle":"0","Td":"45","Ti":"180","aug":"0","fan":"1","ign":"0","mode":"Shutdown","pgm":"0","target":"90","u":"1"}
{"Cycle":"20","LReadPgm":"1486232986","LReadWeb":"1486233856","LWritten":"1486233844","PB":"60","PMode":"2","PToggle":"0","Td":"45","Ti":"180","aug":"0","fan":"1","ign":"0","mode":"Shutdown","pgm":"0","target":"90","u":"1"}
{"Cycle
{"Cycle":"20","LReadPgm":"1486232986","LReadWeb":"1486233856","LWritten":"1486233844","PB":"60","PMode":"2","PToggle":"0","Td":"45","Ti":"180","aug":"0","fan":"1","ign":"0","mode":"Shutdown","pgm":"0","target":"90","u":"1"}
{"Cycle":"20","LReadPgm":"1486232986","LReadWeb":"1486233856","LWritten":"1486233844","PB":"60","PMode":"2","PToggle":"0","Td":"45","Ti":"180","aug":"0","fan":"1","ign":"0","mode":"Shutdown","pgm":"0","target":"90","u":"1"}
{"Cycle
{"Cycle":"20","LReadPgm":"1486232986","LReadWeb":"1486233856","LWritten":"1486233844","PB":"60","PMode":"2","PToggle":"0","Td":"45","Ti":"180","aug":"0","fan":"1","ign":"0","mode":"Shutdown","pgm":"0","target":"90","u":"1"}
{"Cycle":"20","LReadPgm":"1486232986","LReadWeb":"1486233856","LWritten":"1486233844","PB":"60","PMode":"2","PToggle":"0","Td":"45","Ti":"180","aug":"0","fan":"1","ign":"0","mode":"Shutdown","pgm":"0","target":"90","u":"1"}
{"Cycle

I have now played a bit with your JSON string and as it seems, the SparkJson library gets into troubles once you have more than 15 fields in there and you have 16.

I have opened a GitHub issue on the library repo

@ScruffR, a big Thank You for you help (and mentoring). I’m new to C++ and learning and all of us beginners struggling to be intermediates and beyond appreciate your willingness to coach!

I’ll monitor this thread and github for any guidance… as I move on to my OLED dev!

1 Like