To read a JSON file in Ruby, you need to use the built-in JSON
library. Here are the steps to do it:
json
library:main.rb15 chars2 lines
File.open
:main.rb30 chars2 lines
JSON.parse
:main.rb29 chars2 lines
main.rb11 chars2 lines
Now, the data
variable will hold the parsed JSON data, which you can access like any other Ruby data structure.
Here's an example JSON file and Ruby code to demonstrate:
example.json
56 chars6 lines
example.rb
main.rb143 chars10 lines
Output:
main.rb17 chars4 lines
gistlibby LogSnag