Before you go, check out these stories!

0
Hackernoon logoRuby: How to read/write JSON File by@davomartinez-dev

Ruby: How to read/write JSON File

Author profile picture

@davomartinez-devDavid Martínez

👨🏻‍💻 🔱 | Full Stack Dev | Mechatronics Engineer | 🔱 ⚡️React ⚡️Redux ⚡️Ruby on Rails⚡️

In Ruby read and write JSON file to hash can be achieved using File Handling. 

Example to parse JSON file into a hash and write data into our JSON file is illustrated with this tutorial.

Before getting started, lets talk about what is a JSON file.

In an overview, JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language Standard ECMA-262 3rd Edition - December 1999. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.

If you want to know more about JSON you might want to read Introduction to JSON.

Installation.

If you don't have already installed JSON gem on your computer you can install it running the following command.

gem install json

You can check if everything is installed correctly if it is, you are going to have something like this:

require 'json'
 => true

Reading the file.

In order to read the file, we have to execute the following command, giving the file that we would like to read.

file = File.read('./file-name-to-be-read.json')

Parsing the file into hash.

Then we will have to parse the data from the file using file handle created with name ‘file’ and store it into 'data_hash' variable.

data_hash = JSON.parse(file)

Step - 1

In this case we are going to use the following JSON file to get the data, save into a hash, make modifications and save the changes into the same JSON file.

{
  "author":"Isaac Asimov",
  "url":"https://isaacbooks.com",
  "books":
    {
      "1":"Fantastic Voyage",
      "2":"The Robots of Dawn",
      "3": "Pebble In The Sky"
    }
}

Step - 2

In order to get the file into a proper hash to start working in it, we have to read the file and save into a variable, then we have to parse that variable.

require 'json'
file = File.read('./sample-data.json')
data_hash = JSON.parse(file)

Step - 3

Next we are going to make some changes into the hash we already have saved into 'data_hash' variable.

data_hash['books']['1'] = 'I, Robot'
data_hash['books']['2'] = 'The Caves of Steel'

Step - 4

In order to write the changes into the JSON file, we have to run the write command into the file that is on the root folder or any other location you want.

File.write('./sample-data.json', JSON.dump(data_hash))

After this, we are going to have the following result in our JSON file.

{"author"=>"Isaac Asimov", "url"=>"https://isaacbooks.com", "books"=>{"1"=>"I, Robot", "2"=>"The Caves of Steel", "3"=>"Pebble In The Sky"}}

Conclusion

With this feature, we can easily access and save important data from our program or application following the JSON standard.

This is a quick guide to make the JSON file work in your application / program easily and quickly.

I hope this tutorial is useful to you. If you liked please share it.

I am on GitHub | Twitter LinkedIn

Reference

Author profile picture

@davomartinez-devDavid Martínez

Read my stories

👨🏻‍💻 🔱 | Full Stack Dev | Mechatronics Engineer | 🔱 ⚡️React ⚡️Redux ⚡️Ruby on Rails⚡️

Tags

Join Hacker Noon

Create your free account to unlock your custom reading experience.