title: How can I safely use JSON.parse on a string object that might be a simple string or a string object?
body: <p>I need to be able to parse a string in an object format or in a plain string format. what is the safest way to do this?</p>
  
  <p>I tried JSON.parse(data) but it does not work in case that data is a plain string.</p>
  
  <hr>
  
  <h2>EDIT - The chosen solution</h2>
  
  <p>Thanks to you, this is how I solved the problem:</p>
  
  <pre><code>try {
      dataObj = JSON.parse(data);
  } catch (err) {
      if (typeof data === "object") {
          dataObj = data;
      } else {
          dataObj = {};
      }
  }
  </code></pre>