Processing data from a webhook on an Electron using JsonParserGeneratorRK

Hey folks, I’ve logged this as a bug over at https://github.com/rickkas7/JsonParserGeneratorRK/issues/3 however as the software hasn’t been updated in some months I thought I’d post here as well.

I have JSON returned from a webhook targeting https://postcodes.io/ which looks like this:

  "latitude": 51.827172,
  "longitude": -2.718521,
  "returned_postcode": "AA00 0AA"
}

I’m trying to process this with the following code:

   
    Particle.publish("received_data", data);
    parser.addString(data);
    if (parser.parse()){
      Particle.publish("success","JSON Data Parsed Successfully");
      String returned_postcode = parser.getReference().key("returned_postcode").valueString();
      float lat = parser.getReference().key("latitude").valueFloat();
      float lng = parser.getReference().key("longitude").valueFloat();
      Particle.publish("returned_postcode", returned_postcode);
      Particle.publish("target_latitude", String(lat));
      Particle.publish("target_longitude", String(lng));
    } else {
        Particle.publish("error","could not parse json");
    }
}

but all of the values published to the console return either null in the case of the postcode, or 0.000000 in the case of the lat/lng values.

The parser.parse() code always seems to evaluate as true, as I don’t get to the “could not parse json” output.

I’m sure I’m holding it wrong, but I’m used to working with ArduinoJSON and this feels like a better solution for the Electron

Does your incoming data really look like this without the opening curly braces?

Also if you parser.addString() you should make sure the original string is empty since when you already have a string stored that contains invalid data and add (aka append) to that you'll always ever see the old data and never parse the newly added.

See more elaborate discussion here

Try reading out the JSON buffer via Serial.println(parser.getBuffer());

BTW, you are violating the 1/sec rate limit for Particle.publish() when the condition is satisfied.

I believe the problem is this:

NOTE 2: Particle.publish() and the Particle.subscribe()handler(s) share the same buffer. As such, calling Particle.publish() within a Particle.subscribe() handler will wipe the subscribe buffer! In these cases, copying the subscribe buffer's content to a separate char buffer prior to calling Particle.publish() is recommended.
https://docs.particle.io/reference/device-os/firmware/photon/#particle-subscribe-

When you Particle.publish from a subscribe handler you overwrite the contents of the received data, which is why you can't read the latitude, etc. successfully.

You also need to parser.clear() as ScruffR pointed out.

And you can probably make it work by swapping the first two lines, calling addString() first.

1 Like

Thanks both, that’s brilliant, I’ll update the code later - ironic that my debug Particle.publish() statements are part of the problem… :smiley:

As far as the TX rate is concerned, I’ll be backing that off once I fix it, it’s just for debugging at the moment.