# Save and recall variable in a file

**URL:** <https://discourse.processing.org/t/save-and-recall-variable-in-a-file/37968>\
**Category:** Beginners\
**Created:** [July 15, 2022, 2:07pm UTC](https://discourse.processing.org/t/save-and-recall-variable-in-a-file/37968 "2022-07-15T14:07:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![leuwenn](https://avatars.discourse-cdn.com/v4/letter/l/cdc98d/32.png) [@leuwenn](https://discourse.processing.org/u/leuwenn)\
**Post date:** [July 15, 2022, 2:07pm UTC](https://discourse.processing.org/t/save-and-recall-variable-in-a-file/37968/1 "2022-07-15T14:07:00Z")

</div>

Hi,  
I’m trying to save several variables to a file so I can reload them when the program opens. I manage to create a JSON file but I have to manually add the code, I would like to automate this by adding in an Array, the variables that I would like to save and recall. I managed to save my variables in a JSON file but I can’t extract these values ​​to reassign them to variables.

Old code that is working :

```auto
      json = new JSONObject();
      json.setInt("Mode", Mode);
      json.setInt("PortServeur", PortServeur);
      json.setString("IpRP1", IpRP1);
      json.setInt("PortRP1", PortRP1);
      json.setString("IpRP2", IpRP2);
      json.setInt("PortRP2", PortRP2);
      json.setString("IpRP3", IpRP3);
      json.setInt("PortRP3", PortRP3);
      json.setString("IpRP4", IpRP4);
      json.setInt("PortRP4", PortRP4);
      json.setString("IpLive", IpLive);
      json.setInt("PortLive", PortLive);
      json.setInt("DureePenalite", DureePenalite);
      saveJSONObject(json, "data/config.json");

```

```auto

void setup() {
  // Load JSON
  json = loadJSONObject("config.json");
  Mode = json.getInt("Mode");
  PortServeur = json.getInt("PortServeur");
  IpRP1 = json.getString("IpRP1");
  PortRP1 = json.getInt("PortRP1");
  IpRP2 = json.getString("IpRP2");
  PortRP2 = json.getInt("PortRP2");
  IpLive = json.getString("IpLive");
  PortLive = json.getInt("PortLive");
  ...

```

New code trying to automate (the names of the variables have changed in the meantime) :

```auto
void saveConfig() {
  int[] JsonIntValeur = { Mode, PortServeur, PortBuzzer1, PortBuzzer2, PortBuzzer3, PortBuzzer4, PortLive, PortMoniteur1, PortMoniteur2, DureePenalite};
  String[] JsonStringValeur = { IpBuzzer1, IpBuzzer2, IpBuzzer3, IpBuzzer4, IpLive, IpMoniteur1, IpMoniteur2};

  JSONArray valuesInt = new JSONArray();
  JSONArray valuesString = new JSONArray();

  for (int i = 0; i < JsonIntValeur.length; i++) {
    JSONObject config = new JSONObject();
    config.setInt(JsonIntNom[i], JsonIntValeur[i]);
    valuesInt.setJSONObject(i, config);
  }

  for (int i = 0; i < JsonStringValeur.length; i++) {
    JSONObject config2 = new JSONObject();
    config2.setString(JsonStringNom[i], JsonStringValeur[i]);
    valuesString.setJSONObject(i, config2);
  }

  json = new JSONObject();
  json.setJSONArray("Int", valuesInt);
  json.setJSONArray("String", valuesString);

  saveJSONObject(json, "data/config.json");
}

```

```auto
void loadConfig() {
  int[] JsonIntValeur = { Mode, PortServeur, PortBuzzer1, PortBuzzer2, PortBuzzer3, PortBuzzer4, PortLive, PortMoniteur1, PortMoniteur2, DureePenalite};
  String[] JsonStringValeur = { IpBuzzer1, IpBuzzer2, IpBuzzer3, IpBuzzer4, IpLive, IpMoniteur1, IpMoniteur2};
  json = loadJSONObject("config.json");
  for (int i = 0; i < JsonIntValeur.length; i++) {
    JsonIntValeur[i] = json.getInt(JsonIntNom[i]);
  }
}

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [July 15, 2022, 2:56pm UTC](https://discourse.processing.org/t/save-and-recall-variable-in-a-file/37968/2 "2022-07-15T14:56:00Z")

</div>

> [@leuwenn](#):
>
> , I would like to automate this by adding in an Array,

I see 4 arrays in your attempt, not just 1:

1. _JsonIntNom[]_
2. _JsonIntValeur[]_
3. _JsonStringNom[]_
4. _JsonStringValeur[]_

Can’t you just use the JSONObject itself in place of your many variables?

For example, if you wanna read the “Mode” “variable” you use: `mode = json.getInt("Mode");`.  
If you wanna assign to “Mode” you use: `json.setInt("Mode", mode);`.

---

<div class="post-metadata">

**Author:** ![leuwenn](https://avatars.discourse-cdn.com/v4/letter/l/cdc98d/32.png) [@leuwenn](https://discourse.processing.org/u/leuwenn)\
**Post date:** [July 16, 2022, 1:49pm UTC](https://discourse.processing.org/t/save-and-recall-variable-in-a-file/37968/3 "2022-07-16T13:49:14Z")

</div>

Indeed, I had to make two Array for the values, one for the int and one for the strings. Which I doubled with an array where I have the variable name. I could have used maybe a hashmap or a table.

Is it possible to extract the variables from the array?  
without rewriting this formula for all the values…  
`mode = json.getInt("Mode");`  
With a “for loop” ?  
Otherwise indeed, I replace all my variables of my program by references to this array. Is this a generalized practice? I’m new to programming, I don’t have the best practices yet

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [July 16, 2022, 2:27pm UTC](https://discourse.processing.org/t/save-and-recall-variable-in-a-file/37968/4 "2022-07-16T14:27:37Z")

</div>

> [@leuwenn](#):
>
> Is it possible to extract the variables from the array?

The only way Java would allow us to access fields from a class via an array of String names is by using [reflection](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/reflect/package-summary.html) (a.K.a. introspection), which would be an over-the-top solution btW.

> [@leuwenn](#):
>
> I could have used maybe a HashMap or a Table.

Both containers would provide access to their content very similar to how JSONObject or JSONArray do already.

That is, you’d still use **get()**-like methods w/ a String argument to get a value from them.

IMO you should stick w/ JSONObject and treat its entries as if they were actual variables.

In order to make things safer for such approach, don’t declare _Mode_, _PortServeur_, _IpLive_, etc. as global variables.

Instead declare them as local variables whenever reading from the JSONObject container like this:

- `int mode = json.getInt("Mode");`
- `String ipLive = json.getString("IpLive");`

And if you happen to update the value on any of those local variables, don’t forget to write it back to their corresponding JSONObject entry after finishing:

```auto
int mode = json.getInt("Mode");
String ipLive = json.getString("IpLive");

mode -= 5;
mode *= 3;
ipLive += mode;

json.setInt("Mode", mode);
json.setString("IpLive", ipLive);

```

That’s pretty much it.

---

<div class="post-metadata">

**Author:** ![leuwenn](https://avatars.discourse-cdn.com/v4/letter/l/cdc98d/32.png) [@leuwenn](https://discourse.processing.org/u/leuwenn)\
**Post date:** [July 16, 2022, 5:50pm UTC](https://discourse.processing.org/t/save-and-recall-variable-in-a-file/37968/5 "2022-07-16T17:50:08Z")

</div>

Thanks for your help !
