Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6903073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:52:47+00:00 2026-05-27T07:52:47+00:00

I received this error, and I couldn’t find any reasonable answer to this question,

  • 0

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.

If you run this snippet in irb:

JSON.parse( nil )

You’ll see the following error:

TypeError: can't convert nil into String

I was kind of expecting the function to return nil, and not a TypeError. If you convert all input using to_s, then you’ll see the octet error:

JSON::ParserError: A JSON text must at least contain two octets!

That’s just fine and well. If you don’t know what an octet is, read this post for a summary and solution:
What is a JSON octet and why are two required?

Solution

The variable you’re passing in is an empty string. Don’t attempt to use an empty string in the JSON.parse method.

Question

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 nil values. Any suggestions would be greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T07:52:48+00:00Added an answer on May 27, 2026 at 7:52 am
    parsed = json && json.length >= 2 ? JSON.parse(json) : nil
    

    But really the library should be able to handle this case and return nil. Web browsers with built-in JSON support seem to work just like you expect after all.


    Or to do it with a only slightly intrusive mini patch:

    module JSON
      def self.parse_nil(json)
        JSON.parse(json) if json && json.length >= 2
      end
    end
    
    parsed = JSON.parse_nil(json)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I received this error when trying to run a build from within VS2010. C:\Program
I'm using subclipse in Flex Builder 3, and recently received this error when trying
This is a question I asked on another forum which received some decent answers,
So this morning I posted a confused question about assembly and I received some
This is a follow-up to a previous question I had about interfaces. I received
Has anyone run into this error message before when using a timer on an
Hey all, I received a termination on my app and this error in the
When trying to un-install Git on Windows 7, I received this error message: Runtime
I have a problem with this code. You can find all classes here .
I have added some code which compiles cleanly and have just received this Windows

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.