title: A JSON text must at least contain two octets
body: <p>I received this error, and I couldn't find any reasonable answer to this question, so I thought I'd write a summary of the problem.</p>
  
  <p>If you run this snippet in irb:</p>
  
  <pre><code>JSON.parse( nil )
  </code></pre>
  
  <p>You'll see the following error:</p>
  
  <pre><code>TypeError: can't convert nil into String
  </code></pre>
  
  <p>I was kind of expecting the function to return <code>nil</code>, and not a <code>TypeError</code>. If you convert all input using <code>to_s</code>, then you'll see the octet error:</p>
  
  <pre><code>JSON::ParserError: A JSON text must at least contain two octets!
  </code></pre>
  
  <p>That's just fine and well. If you don't know what an octet is, read this post for a summary and solution:
  <a href="https://stackoverflow.com/questions/7200554/what-is-json-octet-and-why-are-two-required">What is a JSON octet and why are two required?</a></p>
  
  <p><strong>Solution</strong></p>
  
  <p>The variable you're passing in is an empty string. Don't attempt to use an empty string in the <code>JSON.parse</code> method.</p>
  
  <p><strong>Question</strong></p>
  
  <p>So, now I know the cause of the error, what pattern should I use to handle this?  I'm a bit loathe to monkey patch the JSON library to allow <code>nil</code> values. Any suggestions would be greatly appreciated.</p>