Open In App

How to parse JSON in Ruby?

JSON stands for JavaScript Object Notation. In the JSON the data is stored in the form of a key-value pair. we can parse the JSON file in various languages with the help of the respective modules available in the languages. Let's suppose when we want to parse a JSON file in the Python programming language then we use some of the libraries like JSON and Pandas. In Ruby to parse the Json we need to import the required libraries such as "JSON" using the required keyword in the ruby.

We can read the JSON string as well as the JSON file in the ruby using the 'JSON' in the ruby.

1) Parsing the JSON string in Ruby:

In the below program, we import the 'Json' module using the 'require' keyword. we create a json string and then use the parse method in the json(). The parse() method parses the json string and then returns the json string. After that, we can access the values using the names of the keys in the ruby.

Program to parse the JSON string in Ruby:

#importing the JSON Module 
require 'json'
data = '{"Name" : "Harsha" ,"Subject":{"1":"Physics","2":"Maths"}}'
json_string = JSON.parse(data)
#Parsing the json string in the ruby 
puts json_string["Name"]
#Printing one of the key value in the Json string 
puts json_string["Subject"]["1"]

Output
Harsha
Physics

Program Explaination :

2) Parsing the json file in ruby:

In the below code we will parse a json file by giving the path of the json file using the open() method the open() method in the ruby helps us to open the file then we read the contents of the file in the ruby using the read in the ruby . After reading the contents from the file the contents in the file will be stored in the object . Then we pass the contents object into the parse method which is available in the ruby json .

Program to parsing the json file in ruby:

file_name.json: {"name" : "Harsha" , "subject" : "Maths" , "Roll" :{"1":"Harsha" , "2" : "Vardhan" , "3" : "Krishna" } }

#Importing the module json 
require "json"

#Opening the File named "file_name.json"
file = open("file_name.json")

#Reading the contents from the file 
parsed = file.read 

#Parsing the json  contents 
new_string = JSON.parse(parsed)

#Printing the contents 
puts new_string

#Itearting over each elements in the JSON String 
for i in new_string 
    puts i    
end   

Output

{"name"=>"Harsha", "subject"=>"Maths", "Roll"=>{"1"=>"Harsha", "2"=>"Vardhan", "3"=>"Krishna"}}
name
Harsha
subject
Maths
Roll
{"1"=>"Harsha", "2"=>"Vardhan", "3"=>"Krishna"}

Program Explaination:

In this way we can parse the json file which is available in the ruby.

Conclusion:

Generally we use many formats in the files to store the data we use the formats like excel and json and csv to store the data within the files .Generally we use the json files to store the data because we can easily interchange the data in the json file and it is easily understandable to the humans and machines can easily parse the data . Generally we prefer the json formats among all the formats because the data communication easily takes place between the API's and applications .

Article Tags :