Parsing from a JSON file in Ruby and Extract numbers from Nested Hashes

Using the JSON class:

Importing a file:

require "json"
file = File.open "/path/to/your/file.json"
data = JSON.load file

Optionally, you can close it now:

file.close

The file looks like this:

{
  "title": "Facebook",
  "url": "https://www.facebook.com",
  "posts": [
    "lemon-car",
    "dead-memes"
  ]
}

The file is now able to be read like this:

data["title"]
=> "Facebook"
data.keys
=> ["title", "url", "posts"]
data['posts']
=> ["lemon-car", "dead-memes"]
data["url"]
=> "https://www.facebook.com"

Hope this helped!

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)