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 6719831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:08:21+00:00 2026-05-26T09:08:21+00:00

Why can Ruby’s built-in JSON not deserialize simple JSON primitives, and how do I

  • 0

Why can Ruby’s built-in JSON not deserialize simple JSON primitives, and how do I work around it?

irb(main):001:0> require 'json'
#=> true

irb(main):002:0> objects = [ {}, [], 42, "", true, nil ]
#=> [{}, [], 42, "", true]

irb(main):012:0> objects.each do |o|
irb(main):013:1*   json = o.to_json
irb(main):014:1>   begin
irb(main):015:2*     p JSON.parse(json)
irb(main):016:2>   rescue Exception => e
irb(main):017:2>     puts "Error parsing #{json.inspect}: #{e}"
irb(main):018:2>   end
irb(main):019:1> end
{}
[]
Error parsing "42": 706: unexpected token at '42'
Error parsing "\"\"": 706: unexpected token at '""'
Error parsing "true": 706: unexpected token at 'true'
Error parsing "null": 706: unexpected token at 'null'
#=> [{}, [], 42, "", true, nil]

irb(main):020:0> RUBY_DESCRIPTION
#=> "ruby 1.9.2p180 (2011-02-18 revision 30909) [x86_64-darwin10.7.0]"
irb(main):022:0> JSON::VERSION
#=> "1.4.2"
  • 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-26T09:08:22+00:00Added an answer on May 26, 2026 at 9:08 am

    RFC 4627: The application/json Media Type for JavaScript Object Notation (JSON) has this to say:

    2.  JSON Grammar
    
       A JSON text is a sequence of tokens.  The set of tokens includes six
       structural characters, strings, numbers, and three literal names.
    
       A JSON text is a serialized object or array.
    
          JSON-text = object / array
    
    [...]
    
    2.1.  Values
    
       A JSON value MUST be an object, array, number, or string, or one of
       the following three literal names:
    
          false null true
    

    If you call to_json on your six sample objects, we get this:

    >> objects = [ {}, [], 42, "", true, nil ]
    >> objects.map { |o| puts o.to_json }
    {}
    []
    42
    ""
    true
    null
    

    So the first and second are valid JSON texts whereas the last four are not valid JSON texts even though they are valid JSON values.

    JSON.parse wants what it calls a JSON document:

    Parse the JSON document source into a Ruby data structure and return it.

    Perhaps JSON document is the library’s term for what RFC 4627 calls a JSON text. If so, then raising an exception is a reasonable response to an invalid input.

    If you forcibly wrap and unwrap everything:

    objects.each do |o|
        json = o.to_json 
        begin
            json_text = '[' + json + ']'
            p JSON.parse(json_text)[0]
        rescue Exception => e 
            puts "Error parsing #{json.inspect}: #{e}"    
        end    
    end
    

    And as you note in your comment, using an array as the wrapper is better than an object in case the caller wants to use the :symbolize_names option. Wrapping like this means that you’ll always be feeding JSON.parse a JSON text and everything should be fine.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Can I add custom methods/attributes to built-in Python types? In Ruby you
Can ruby manipulate and work with peripherals like webcams? I would like to create
Ruby can add methods to the Number class and other core types to get
I'm trying to install some Ruby Gems so I can use Ruby to notify
In Ruby you can reference variables inside strings and they are interpolated at runtime.
In Ruby I can use result << (yield element) and everything works, but if
In Ruby you can write a rescue at the end of an assignment to
Question: How can I document Ruby code using Doxygen? Disclaimer: I know ruby already
Using Sinatra in Ruby you can set the server's settings by doing: set :myvariable,
is there any way I can reproduce this ruby function: def Password.hash(password,salt) Digest::SHA512.hexdigest(#{password}:#{salt}) end

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.