Question about JsonParserGeneratorRK and How to Deal With Memory of JSON Object

Hello!
I am currently using the JsonParserGeneratorRK library to send data to the cloud in the form of JSON packages. I want to store a battery voltage over a period of time and then send that to the cloud. For example, a timer in the Main() loop stores the batt value every 10 minutes. Each value is stored as a key in a JSON object (using the library). After the 10th key has populated the object, the oldest key is deleted and the newest key is added. My goal is to keep the JSON Object into a manageable size for to use over Cellular network.

What is the easiest way to do that?

Thanks for your help!

This is an interesting use case. While you can do it with the existing library, I created version 0.1.3 with four new methods to make this easier.

  • Added JsonWriter methods insertKeyArray() and insertKeyVector() to make it easier to add arrays.
  • Added JsonWriter methods insertArray() and insertVector() to make it easier to add arrays.

The assumption here is that you’ll have an object that conceptually looks like:

{
  "a":"test",
  "b":[
     100.00,
     99.99,
     99.90
  ]
}

Where “a” is could be any number of keys with any kind of data.

The “b” key is an array of floats. If you wanted to record both a timestamp and a value “b” would need to be an object instead which would be a little more complicated, but still possible.

You’d store your array of float (battery SoC) as a global variable like:

std::vector<float> batteryCharge;

And when you add an element every 10 minutes, you’d do something like:

batteryCharge.push_back(System.batteryCharge();
if (batteryCharge.size() > 10) {
    // Remove the first (oldest) element
    batteryCharge.erase(batteryCharge.begin());
}

And this is the code to generate the publish:

jw.startObject();

jw.setFloatPlaces(2);
jw.insertKeyValue("a", "test");
jw.insertKeyVector("b", batteryCharge);

jw.finishObjectOrArray();

The full example code used to test this is in test/JsonTest.cpp, search for “Writer test - float vector #2”.

1 Like

Thanks Rickkas7. The update you made to your library was a lifesaver. I have successfully solved my issue thanks to your help!