title: Parsing a JSON string from within a TXT file
body: <p>I'm wanting to have the user pick a file which is then read and then parsed into a JSON in order to be added to their localStorage. The issue is that reading from the file results in every single character being read as a key, however pasting the JSON string directly into the function results in the string being properly parsed.</p>
  <p>Is anyone able to tell me what why the file method isn't working?</p>
  <p>Here's the code that reads the file (This results in every single character becoming a key within LocalStorage).</p>
  <pre><code>&lt;h1&gt;Import/Export&lt;/h1&gt;
          &lt;button id=&quot;export&quot;&gt;Export&lt;/button&gt;
          &lt;br&gt;&lt;br&gt;
          &lt;input type=&quot;file&quot; name=&quot;inputfile&quot; id=&quot;inputfile&quot;&gt;
          &lt;br&gt;&lt;br&gt;
          &lt;!-- &lt;textarea id=&quot;input&quot; placeholder=&quot;Paste the contents of the exported TXT file here&quot;&gt;&lt;/textarea&gt; --&gt;
          &lt;br&gt;
          &lt;br&gt;
          &lt;button id=&quot;import&quot;&gt;Import&lt;/button&gt;
          &lt;pre id=&quot;output&quot; style=&quot;display: none;&quot;&gt;&lt;/pre&gt;
          
          &lt;script type=&quot;text/javascript&quot;&gt;
              document.getElementById('inputfile').addEventListener('change', function() {
                  var fr=new FileReader();
                  fr.onload=function(){
                      document.getElementById('output').textContent=fr.result;
                      console.log(fr.result)
                      // Read the JSON
                      var data = JSON.parse(fr.result);
                      console.log(data)
                      Object.keys(data).forEach(function (k) {
                          localStorage.setItem(k, data[k]);
                      });
                  }
                  fr.readAsText(this.files[0])
              })
          &lt;/script&gt;
  </code></pre>
  <p>Here's the code that just works either within a function or running it in the console:</p>
  <pre><code>var data = JSON.parse(&quot;JSON STRING GOES HERE&quot;);
      console.log(data)
      Object.keys(data).forEach(function (k) {
          localStorage.setItem(k, data[k]);
      });
  </code></pre>