title: Create, Write, Edit JSON file in Android Studio
body: <p>I would like to create a JSON file in the internal storage of the phone, to store data.
  I want to be able to add objects (&quot;configX&quot;) to the file and then read the data.</p>
  <p>It should look something like this:</p>
  <pre><code>{
  
    &quot;config1&quot;: {
  
      &quot;component1&quot;: &quot;url&quot;,
      &quot;component2&quot;: &quot;url&quot;,
      &quot;component3&quot;: &quot;url&quot;
  
    },
  
    &quot;config2&quot;: {
  
      &quot;component1&quot;: &quot;url&quot;,
      &quot;component2&quot;: &quot;url&quot;,
      &quot;component3&quot;: &quot;url&quot;
  
    }
  
  }
  </code></pre>
  <p>I can create a JSON file like this :</p>
  <pre><code>public void saveToJson(){
      JSONObject json = new JSONObject();
      try {
          json.put(&quot;component1&quot;, &quot;url&quot;);
          json.put(&quot;component2&quot;, &quot;url&quot;);
  
          String jsonString = json.toString();
          
          FileOutputStream fos = this.openFileOutput(&quot;jsonfile&quot;, Context.MODE_PRIVATE);
          fos.write(jsonString.getBytes());
          fos.close();
  
          Log.d(&quot;JSON&quot; , json.toString());
  
      } catch (IOException | JSONException e) {
          e.printStackTrace();
      }
  }
  </code></pre>
  <p>But how to put the components in the config object ? And how to retrieve the data ?</p>
  <p><strong>EDIT 1 :</strong></p>
  <p><a href="https://stackoverflow.com/a/62474912/11652860">https://stackoverflow.com/a/62474912/11652860</a></p>
  <p>Thanks for the very detailed answer, I'm doing something wrong. I have an Activity where I put and save data to the json file:</p>
  <pre><code>public class Data {
  
          private Map&lt;String, Map&lt;String, String&gt;&gt; map;
  
          public Data() {
  
          }
  
          public Data(Map&lt;String, Map&lt;String, String&gt;&gt; map) {
              this.map = map;
          }
  
          public Map&lt;String, Map&lt;String, String&gt;&gt; getMap() {
              return map;
          }
  
          public void setMap(Map&lt;String, Map&lt;String, String&gt;&gt; map) {
              this.map = map;
          }
  
      }
  </code></pre>
  <pre><code>Map&lt;String, String&gt; config1 = new HashMap&lt;&gt;();
  config1.put(&quot;component1&quot;, &quot;url1&quot;);
  config1.put(&quot;component2&quot;, &quot;url1&quot;);
  config1.put(&quot;component3&quot;, &quot;url1&quot;);
  
  Map&lt;String, Map&lt;String, String&gt;&gt; map = new HashMap&lt;&gt;();
  map.put(&quot;config1&quot;, config1);
  
  Data data = new Data(map);
  
  Gson gson = new Gson();
  String json = gson.toJson(data);
  
  FileOutputStream fos = null;
   try {
  fos = webViewActivity.this.openFileOutput(&quot;jsonfile&quot;, Context.MODE_PRIVATE);
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  }
   try {
   fos.write(json.getBytes());
  } catch (IOException e) {
    e.printStackTrace();
  }
   try {
   fos.close();
  } catch (IOException e) {
   e.printStackTrace();
  }
  </code></pre>
  <p>And a fragment where I load the data :</p>
  <pre><code>public class Data {
  
          private Map&lt;String, Map&lt;String, String&gt;&gt; map;
  
          public Data() {
  
          }
  
          public Data(Map&lt;String, Map&lt;String, String&gt;&gt; map) {
              this.map = map;
          }
  
          public Map&lt;String, Map&lt;String, String&gt;&gt; getMap() {
              return map;
          }
  
          public void setMap(Map&lt;String, Map&lt;String, String&gt;&gt; map) {
              this.map = map;
          }
  
      }
  </code></pre>
  <pre><code> public void load(){
          FileInputStream fis = null;
  
          try {
  
              fis = getContext().openFileInput(&quot;jsonfile.txt&quot;);
              InputStreamReader isr = new InputStreamReader(fis);
              BufferedReader br = new BufferedReader(isr);
  
              StringBuilder sb = new StringBuilder();
              String text;
  
              while ((text = br.readLine()) != null){
                  sb.append(text).append(&quot;\n&quot;);
  
                  Gson gson = new Gson();
  
                  String json = gson.toJson(text);
                  Data data = gson.fromJson(json, Data.class);
  
                  String url = data.getMap().get(&quot;config1&quot;).get(&quot;component1&quot;);
  
                  frameTV.setText(url);
  
              }
  
  
          } catch (FileNotFoundException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          } finally {
              if (fis != null){
                  try {
                      fis.close();
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
              }
          }
      }
  </code></pre>
  <p>The saving and loading parts must be wrong, but they worked for getting text out a text file</p>
  <p><strong>EDIT 2 :</strong></p>
  <p>I found the problem, I wasn't loading and saving properly :</p>
  <p>SAVING:</p>
  <pre><code>String filename = &quot;jsonfile.txt&quot;;
  
  FileOutputStream outputStream;
  
  try {
     outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
     outputStream.write(json.getBytes());
     outputStream.close();
  } catch (Exception e) {
     e.printStackTrace();
  }
  </code></pre>
  <p>LOADING :</p>
  <pre><code>FileInputStream fis = getContext().openFileInput(&quot;jsonfile.txt&quot;);
  InputStreamReader isr = new InputStreamReader(fis);
  BufferedReader bufferedReader = new BufferedReader(isr);
  StringBuilder sb = new StringBuilder();
  String line;
  while ((line = bufferedReader.readLine()) != null) {
      sb.append(line);
  }
  
  String json = sb.toString();
  
  Gson gson = new Gson();
  Data data = gson.fromJson(json, Data.class);
  String priceURL = data.getMap().get(&quot;config1&quot;).get(&quot;url1&quot;);
  </code></pre>
  <p><strong>EDIT 3 :</strong></p>
  <p>My problem now is that I need to create the file once and then check if the file exists, if it does I need to check if config1 exists if it doesn't I need to put config in the file.</p>
  <p>But I can't check if config1 exists because I get : <code>java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.Map com.a.app.ui.app.appFragment$Data.getMap()</code></p>
  <p>I check if it exists by doing :</p>
  <pre><code>Boolean configTest = data.getMap().containsKey(&quot;config1&quot;);
  if(!configTest){}
  </code></pre>
  <p>How can I create the file and check the data without getting a NullPointerException ?</p>
  <p>Thank you for helping me !</p>