# Processing data from a webhook on an Electron using JsonParserGeneratorRK

**URL:** <https://community.particle.io/t/processing-data-from-a-webhook-on-an-electron-using-jsonparsergeneratorrk/45048>\
**Category:** Troubleshooting\
**Created:** [November 7, 2018, 12:44pm UTC](https://community.particle.io/t/processing-data-from-a-webhook-on-an-electron-using-jsonparsergeneratorrk/45048 "2018-11-07T12:44:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pf123](https://avatars.discourse-cdn.com/v4/letter/p/49beb7/32.png) [@pf123](https://community.particle.io/u/pf123)\
**Post date:** [November 7, 2018, 12:44pm UTC](https://community.particle.io/t/processing-data-from-a-webhook-on-an-electron-using-jsonparsergeneratorrk/45048/1 "2018-11-07T12:44:50Z")

</div>

Hey folks, I’ve logged this as a bug over at [https://github.com/rickkas7/JsonParserGeneratorRK/issues/3](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/](https://postcodes.io/) which looks like this:

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

```

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

```auto
   
    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

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [November 7, 2018, 12:56pm UTC](https://community.particle.io/t/processing-data-from-a-webhook-on-an-electron-using-jsonparsergeneratorrk/45048/2 "2018-11-07T12:56:30Z")

</div>

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

> [@Call function in loop](https://community.particle.io/t/call-function-in-loop/43818/9):
>
> This would suggest the jsonParser has some issues validating the incoming data. In order to locate issues with your code one of the steps you always should take first is to add as many Serial.print() statements as you can to see what your code is internally doing and where it’s failing to do what you want. e.g. void myHandler(const char \*event, const char \*data) { bool b; Serial.printlnf("%s: %s", event, data); b = jsonParser.addString(data); Serial.printlnf("addString() ret…

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.

---

<div class="post-metadata">

**Author:** ![rickkas7](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/rickkas7/32/8681_2.png) [@rickkas7](https://community.particle.io/u/rickkas7)\
**Post date:** [November 7, 2018, 1:24pm UTC](https://community.particle.io/t/processing-data-from-a-webhook-on-an-electron-using-jsonparsergeneratorrk/45048/3 "2018-11-07T13:24:00Z")

</div>

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-](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.

---

<div class="post-metadata">

**Author:** ![pf123](https://avatars.discourse-cdn.com/v4/letter/p/49beb7/32.png) [@pf123](https://community.particle.io/u/pf123)\
**Post date:** [November 7, 2018, 4:46pm UTC](https://community.particle.io/t/processing-data-from-a-webhook-on-an-electron-using-jsonparsergeneratorrk/45048/4 "2018-11-07T16:46:44Z")

</div>

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

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.
