Open In App

How to Convert String to JSON in Ruby?

Last Updated : 02 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will learn how to convert a string to JSON in Ruby. String to JSON conversion consists of parsing a JSON-formatted string into a corresponding JSON object, enabling structured data manipulation and interoperability within Ruby applications.

Converting String to JSON in Ruby

Below are the possible approaches to convert string to JSON in Ruby.

Approach 1: Using json.parse

  1. In this approach, we are using the JSON.parse method in Ruby, which is part of the json library.
  2. This method takes a JSON-formatted string (json_string) as input and converts it into a Ruby hash (json_object), effectively parsing the JSON data structure and making it accessible as a native Ruby object.

In the below example, json.parse is used to convert string to JSON in Ruby.

Ruby
require 'json'
json_string = '{"name": "GFG", "age": 30, "city": "Noida"}'
json_object = JSON.parse(json_string)
puts json_object

Output:

Screenshot-2024-03-27-at-15-52-12-428gwm7q6---Ruby---OneCompiler

Approach 2: Using eval Method

  1. In this approach, we are using the eval method in Ruby to directly evaluate the JSON-formatted string (json_string) as Ruby code.
  2. This method treats the string as executable code, effectively converting it into a Ruby hash (json_object).

In the below example, eval method is used to convert string to JSON in Ruby.

Ruby
json_string = '{"name": "GFG", "age": 30, "city": "Noida"}'
json_object = eval(json_string)
puts json_object

Output:

Screenshot-2024-03-27-at-15-54-44-428gwm7q6---Ruby---OneCompiler

Approach 3: Using yaml.load

  1. In this approach, we are using the YAML.load method in Ruby after requiring the ‘yaml’ library.
  2. Although YAML is primarily used for its own format, it can also parse JSON-like strings with basic structures.

In the below example,yaml.load is used to convert string to JSON in Ruby.

Ruby
require 'yaml'
json_string = '{"name": "GFG", "age": 30, "city": "Noida"}'
json_object = YAML.load(json_string)
puts json_object

Output:

Screenshot-2024-03-27-at-15-52-12-428gwm7q6---Ruby---OneCompiler


Similar Reads

How to Convert JSON to Hash in Ruby?
In this article, we will discuss how to convert JSON to hash in Ruby. We can convert JSON to hash through different methods ranging from parsing JSON and JSON.load to using YAML.safe_load Method. Table of Content Using JSON.parse MethodUsing JSON.load MethodUsing YAML.safe_load Method Using JSON.parse MethodThe JSON.parse method is a built-in metho
2 min read
How to Convert Hash to JSON in Ruby?
Ruby Hash is an unordered set of data in key-value pairs. These are mutable and can store multiple data types. JSON stands for Javascript Object Notation and is a human-readable file format that is commonly used in web services and API calls. In this article, we will discuss how to convert a hash to a JSON in Ruby. Converting hash to JSON makes it
2 min read
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
4 min read
How to Work with JSON Data in Ruby?
JSON, which stands for JavaScript Object Notation, is a format used to exchange data between different systems. It is popular because it is easy for both computers and humans to understand. This article focuses on discussing working with JSON data in Ruby. Table of Content Installing the JSON GemParsing JSON in RubyGenerating JSON in RubyJSON with
3 min read
Difference between Ruby and Ruby on Rails
1. Ruby : Ruby is an object-oriented scripting language launched in 1995 and is known as a general-purpose programming language. It was programmed in C programming language. Ruby is a secured programming language and its syntax is similar to Perl and Python. It was developed on the principle of user interface design and it is mainly used to develop
3 min read
How to convert String to Hash in Ruby?
In this article, we will discuss how to convert string to hash in ruby. We can convert a string to a hash through different methods ranging from parsing JSON and parsing YAML to custom string parsing. Table of Content Converting string to hash using JSON parsingConverting string to hash using YAML parsingCustom String Parsing to HashConverting stri
2 min read
How to convert String to Boolean in Ruby?
In this article, we will learn how to convert string to Boolean in Ruby using various methods. Let’s understand each of them with the help of examples. Table of Content Converting string to Boolean using String#casecmpConverting string to Boolean using String#downcaseConvert string to Boolean using Regular ExpressionsConverting string to Boolean us
3 min read
How to Convert a String to Lower or Upper Case in Ruby?
In this article, we will discuss how to convert a string to lower or upper case in Ruby. We can convert a string to lower or upper case using built-in methods provided in the Ruby language. Table of Content Using DowncaseUsing UpcaseUsing CapitalizeUsing DowncaseThe downcase method is used to convert the string to lowercase. Syntax: string.downcase
2 min read
How to convert a string to a constant in Ruby?
In this article, we will learn how to convert a string to a constant in Ruby. Conversion of string to constant consists of dynamically defining a new class or module using the string's value as the constant name, allowing for runtime creation and usage of the constant in Ruby code. Convert a String to a Constant in RubyBelow are the possible approa
2 min read
How to convert a number to Binary in Ruby?
In Ruby, converting a number to binary involves utilizing built-in methods or implementing custom algorithms. Binary representation is a base-2 numeral system, where each digit can be either 0 or 1. There are many ways to convert a number to binary in Ruby like: Using String Interpolation with %b FormatUsing Integer#to_s with Base ArgumentUsing Bit
2 min read
Article Tags :