JSON-content that changes every second

Hello Guys,

I’m working on a project where I receive MQTT messages every second as a char pointer but the message is written as a JSON Format. I then parse the char pointer to a real JSON Foramt and then I want to get one specific thing of the JSON. To do that I worte the following function:

StaticJsonBuffer<200> json_message;

int get_message_info(char* message, char* get_type){
  json_message.clear();
  JsonObject &root = json_message.parseObject(message);

  //If the parse was successfull 
  if (!root.success()) {
    Serial.println("==>FAILURE: parseObject()<==");
    return 0;
  }

  return root[get_type];
}

But I have read that it is very bad to use the staticJson Buffer more than one time and clearing it does memory leaks.

Does somebody know how make a JSON Object that can get refilled every second without causing memory leaks?

Have a look here.

It says that the DynamicBuffer is slow, but as you seem to need a quite small size, it should be ok:

A DynamicJsonBuffer calls malloc() to allocate its memory, and it may have to do this several times if it needs to grow. However, you can specify an initial size to the constructor, so as to make sure that the buffer is big enough and that no further allocation will be needed.

Thank you for the reply.
But the webside says reusable: “no” to the DynamicBuffer does that mean that I can t parse or fill the JsonObject again or does that mean something else ? :smiley:

Try it, or read the manual, I'm afraid I don't know