Global JsonObject (ArduinoJson) I know a global JsonObject shouldn't be used, but I have my reasons.

I have a JsonObject* functions and I want him to save the content of a file.
With


functions=new JsonObject(jsonBuffer.as&lt;JsonObject&gt;());


I can access the JsonObject, but not when I leave the method. Can someone help me to understand why?
(Don't give me a solution like: copy into a struct, I need a JsonObject ready to be read in all my code.

Full code:

JsonObject* functions;

void loadFunctions() {
  File file = SPIFFS.open("/functions.json", "r");
  if (!file) {
    Serial.println("Failed to open the file");
  }else{
    size_t size = file.size();
    if (size &gt; 512) {
      Serial.println("File size is too large");
    }else{
      std::unique_ptr&lt;char[]&gt; buf(new char[size]);
      file.readBytes(buf.get(), size);
      DynamicJsonDocument jsonBuffer(256);
      DeserializationError error = deserializeJson(jsonBuffer, buf.get());
      if (error) {
        Serial.println("Failed to parse file ");
      } else {
        functions=new JsonObject(jsonBuffer.as&lt;JsonObject&gt;());
        Serial.println((*functions)["test"].as&lt;String&gt;()); //this doesn't work in another method
      }
    }
  }
}
jsonBuffer is local to the function. When you leave the function the contents of jsonBuffer are lost. 

functions just points to the internals of jsonBuffer to get its data (to save memory).

You can either:


Make the jsonBuffer global so it sticks around all the time, or
Have a secondary DynamicJsonDocument or StaticJsonDocument that is global to which you copy your objects once read from jsonBuffer.