# ArduinoJson save/load variable to config.json individually (ver 5 or 6)

**URL:** <https://forum.arduino.cc/t/arduinojson-save-load-variable-to-config-json-individually-ver-5-or-6/630291>\
**Category:** Programming\
**Created:** [January 9, 2020, 8:29pm UTC](https://forum.arduino.cc/t/arduinojson-save-load-variable-to-config-json-individually-ver-5-or-6/630291 "2020-01-09T20:29:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![daemons](https://avatars.discourse-cdn.com/v4/letter/d/eb8c5e/32.png) [@daemons](https://forum.arduino.cc/u/daemons)\
**Post date:** [January 9, 2020, 8:29pm UTC](https://forum.arduino.cc/t/arduinojson-save-load-variable-to-config-json-individually-ver-5-or-6/630291/1 "2020-01-09T20:29:22Z")

</div>

Hi there,  
I am building a relatively simple solution to save variables/values to spiffs via arduinojson.

In nutshell:  
The default variables/values are saved to config.json file while the esp8266 in function, though web interface. The next reboot the new variables, like ssid/password are loaded, and connection estabilished. Still i have pleanty of other variables as well, the explanations yet is just for simplicity with ssid/pass.

The default configuration-save/load are working good, but the save for only one variable to conf.json are overwriting the file, so i get the file just with the variable=value which has been saved lastly.

Can someone point me, or hint or help..how to get this part of the code working as expected:  
If variable exists, then update the new value, if not, then create and save.

At the moment the code has been built with arduinojson 5, i dont mind if we need to update to 6.  
Thank in advance,

```cpp
void ConfigFile_Save_Variable(String text, String value) {
  Serial.print("Save_VariableToConfigFile: ");
  Serial.print(text); Serial.print("="); Serial.print(value);

  File configFile = SPIFFS.open("/config.json", "w");
  if (!configFile) {
    Serial.println("- failed to open config file for writing");
    return;
  }
  DynamicJsonBuffer jsonBuffer;
  JsonObject& json = jsonBuffer.createObject();
  //
  json[text] = value;
  //
  //json.printTo(Serial);                    //print to - serial - debug
  json.printTo(configFile);                 //print to - file
  configFile.close();
  Serial.println("");
  Serial.println(" - config.json saved - OK.");
}

```

---

<div class="post-metadata">

**Author:** ![daemons](https://avatars.discourse-cdn.com/v4/letter/d/eb8c5e/32.png) [@daemons](https://forum.arduino.cc/u/daemons)\
**Post date:** [January 10, 2020, 6:45am UTC](https://forum.arduino.cc/t/arduinojson-save-load-variable-to-config-json-individually-ver-5-or-6/630291/2 "2020-01-10T06:45:39Z")

</div>

Meanwhile,  
it seems I have figured it out. had to use an open-write loop with file in the same function.

Updated to arduinojson 6.

```cpp
void ConfigFile_Save_Variable(String VarName, String VarValue) {
  Serial.print("ConfigFile_Save_Variable: ");
  Serial.print(VarName); Serial.print("="); Serial.print(VarValue);

  File configFile = SPIFFS.open("/config.json", "r");
  if (!configFile) {
    Serial.println("- failed to open config file for writing");
    return;
  }

  DynamicJsonDocument doc(2048);
  deserializeJson(doc, configFile);
  serializeJson(doc, Serial);
  configFile.close();
  //
  doc[VarName] = VarValue;
  //
  configFile = SPIFFS.open("/config.json", "w");
  serializeJson(doc, configFile);
  serializeJson(doc, Serial);
  configFile.close();
  Serial.println("");
  Serial.println(" - config.json saved - OK.");

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 6, 2021, 12:14am UTC](https://forum.arduino.cc/t/arduinojson-save-load-variable-to-config-json-individually-ver-5-or-6/630291/3 "2021-05-06T00:14:32Z")

</div>

Closed on May 6, 2021, 12:14am UTC


