# Nodemcu saving & loading JSON in Spiffs

**URL:** <https://forum.arduino.cc/t/nodemcu-saving-loading-json-in-spiffs/884469>\
**Category:** Storage\
**Created:** [July 12, 2021, 4:16pm UTC](https://forum.arduino.cc/t/nodemcu-saving-loading-json-in-spiffs/884469 "2021-07-12T16:16:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![noam76](https://avatars.discourse-cdn.com/v4/letter/n/a587f6/32.png) [@noam76](https://forum.arduino.cc/u/noam76)\
**Post date:** [July 12, 2021, 4:16pm UTC](https://forum.arduino.cc/t/nodemcu-saving-loading-json-in-spiffs/884469/1 "2021-07-12T16:16:26Z")

</div>

Hi, I use Nodemcu V3 and trying to save a JSON file on the SPIFFS.  
I run the saving function after submit my Form and loading them on the setup, with arduinojson 6 library.

- The first step I try with the ArduinoJson --\> JsonConfigFile, it's for an SD card, I try to change it for the SPIFFS.  
After manual reset the controller all data look like without any changing.
- The second step, I check with ESP8266 --\> ConfigFile, the example look like working.  
I don't know after reset because the example saving data in the saveConfig function.

In my code I put the saveConfig() after Form submit and loading them in the setup.  
After manually reset the controller all data still without changing.

I didn't find how to do that successfully.  
My final code I am trying is :

```cpp
#include <ArduinoJson.h>
#include "FS.h"
#include <LittleFS.h>

int Usermax0,Usermax1,Usermax2,Usermax3,Usermax4,Usermax5,Usermax6,Usermax7,Usermax8,Usermax9,Usermax10,Usermax11,Usermax12,Usermax13,Usermax14,Usermax15;
int Usermin0,Usermin1,Usermin2,Usermin3,Usermin4,Usermin5,Usermin6,Usermin7,Usermin8,Usermin9,Usermin10,Usermin11,Usermin12,Usermin13,Usermin14,Usermin15;

bool loadConfiguration() {
  File configFile = LittleFS.open("/config.json", "r");
  if (!configFile) {
    Serial.println("Failed to open config file");
    return false;
  }

  size_t size = configFile.size();
  if (size > 1024) {
    Serial.println("Config file size is too large");
    return false;
  }

  // Allocate a buffer to store contents of the file.
  std::unique_ptr<char[]> buf(new char[size]);

  // We don't use String here because ArduinoJson library requires the input
  // buffer to be mutable. If you don't use ArduinoJson, you may as well
  // use configFile.readString instead.
  configFile.readBytes(buf.get(), size);

  StaticJsonDocument<512> doc;
  auto error = deserializeJson(doc, buf.get());
  if (error) {
    Serial.println("Failed to parse config file");
  Serial.println(error.f_str());
    return false;
  }

  Usermax0 = doc["Usermax0"];
  Usermin0 = doc["Usermin0"];
  Usermax1 = doc["Usermax1"];
  Usermin1 = doc["Usermin1"];
  Usermax2 = doc["Usermax2"];
  Usermin2 = doc["Usermin2"];
  Usermax3 = doc["Usermax3"];
  Usermin3 = doc["Usermin3"];
  Usermax4 = doc["Usermax4"];
  Usermin4 = doc["Usermin4"];
 Usermax5 = doc["Usermax5"];
  Usermin5 = doc["Usermin5"];
  
  Serial.print("Usermax0: "); Serial.println(Usermax0);
  Serial.print("Usermin0: "); Serial.println(Usermin0);
  Serial.print("Usermax1: "); Serial.println(Usermax1);
  Serial.print("Usermin1: "); Serial.println(Usermin1);
  Serial.print("Usermax2: "); Serial.println(Usermax2);
  Serial.print("Usermin2: "); Serial.println(Usermin2);
  Serial.print("Usermax3: "); Serial.println(Usermax3);
  Serial.print("Usermin3: "); Serial.println(Usermin3);
  Serial.print("Usermax4: "); Serial.println(Usermax4);
  Serial.print("Usermin4: "); Serial.println(Usermin4);
  Serial.print("Usermax5: "); Serial.println(Usermax5);
  Serial.print("Usermin5: "); Serial.println(Usermin5);

  configFile.close();
  return true;
}

bool saveConfiguration() {
  StaticJsonDocument<512> doc;

  doc["Usermax0"] = Usermax0;
  doc["Usermin0"] = Usermin0;
  doc["Usermax1"] = Usermax1;
  doc["Usermin1"] = Usermin1;
  doc["Usermax2"] = Usermax2;
  doc["Usermin2"] = Usermin2;
  doc["Usermax3"] = Usermax3;
  doc["Usermin3"] = Usermin3;
  doc["Usermax4"] = Usermax4;
  doc["Usermin4"] = Usermin4;
  doc["Usermax5"] = Usermax5;
  doc["Usermin5"] = Usermin5;

  File configFile = LittleFS.open("/config.json", "w");
  if (!configFile) {
    Serial.println("Failed to open config file for writing");
    return false;
  }

  serializeJson(doc, configFile);
  configFile.close();
  return true;
}
```

---

<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:** [November 9, 2021, 4:17pm UTC](https://forum.arduino.cc/t/nodemcu-saving-loading-json-in-spiffs/884469/2 "2021-11-09T16:17:06Z")

</div>

Closed on November 9, 2021, 4:17pm UTC

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.
