from json to a ruby hash?

You want JSON.parse or JSON.load:

def load_user_lib( filename )
  JSON.parse( )

The key here is to use as a simple way to load the JSON string from disk, so that it can be parsed. Or, if you have UTF-8 data in your file:

  my_object = JSON.parse(, encoding:'utf-8') )

I’ve linked to the JSON documentation above, so you should go read that for more details. But in summary:

  • json = my_object.to_json — method on the specific object to create a JSON string.
  • json = JSON.generate(my_object) — create JSON string from object.
  • JSON.dump(my_object, someIO) — create a JSON string and write to a file.
  • my_object = JSON.parse(json) — create a Ruby object from a JSON string.
  • my_object = JSON.load(someIO) — create a Ruby object from a file.


def load_user_lib( filename ) filename, "r" ) do |f|
    JSON.load( f )

Note: I have used a “snake_case” name for the method corresponding to your “camelCase” saveUserLib as this is the Ruby convention.

Leave a Comment