Sitemap
Jungletronics

Education & STEAM Learning by building. Practical explorations in AI, programming, electronics, robotics, IoT, and other STEAM topics — from ideas and experiments to working products. Follow and build with us.

Fetching JSON Data in Ruby

[Net::HTTP] vs. [HTTParty] vs. [Faraday] Libraries

3 min readMar 29, 2025

--

Press enter or click to view image in full size

In this guide, we’ll compare three ways to fetch and parse JSON data in Ruby:

  • Net::HTTP (standard library, no extra dependencies)
  • HTTParty (simpler and more readable)
  • Faraday (flexible and extensible, commonly used in production apps)

Let’s get Started!

1.Using Net::HTTP (Standard Library)

The Net::HTTP library is part of Ruby’s standard library and allows us to make HTTP requests. We also use the JSON library to parse the response.

Pros: No extra gems required, full control over request handling.
Cons: More verbose, manual parsing required.

📡 Import Required Libraries 🛰️

require 'net/http'
require 'json'

📡 Define the API Endpoint 🛰️

uri = URI.parse(‘https://fakestoreapi.com/users')

This creates a URI object representing the API endpoint.

📡 Make an HTTP GET Request 🛰️

response = Net::HTTP.get_response(uri)

Net::HTTP.get_response(uri) sends a GET request to the API.

It returns an HTTP response object.

📡 Parse the JSON Response 🛰️

data = JSON.parse(response.body)

response.body contains the raw JSON string.

JSON.parse(response.body) converts it into a Ruby array of hashes.

📡 Extract Data 🛰️

emails = data.map { |user| user['email'] } ## extracts all emails.
puts emails
emails = pluck('email')
puts emails

Run in your console:

require 'net/http'
require 'json'
uri = URI.parse('https://fakestoreapi.com/users')
response = Net::HTTP.get_response(uri)
data = JSON.parse(response.body)
emails_0 = data.map { |user| user['email'] } ## extracts all emails.
puts emails_0
emails_1 = data.pluck('email')
puts emails_1

2.Using HTTParty (Simpler API Requests)

Pros: Auto-parses JSON, very easy to use.
Cons: Less flexible for complex requests.

HTTParty is a third-party gem that makes HTTP requests simpler.

Get J3’s stories in your inbox

Join Medium for free to get updates from this writer.

📡 Install the Gem (if not installed) 🛰️

Run this command in the terminal:

gem install httparty

📡 Import the Library 🛰️

require ‘httparty’

📡 Fetch and Parse JSON in One Step 🛰️

base_url = ‘https://fakestoreapi.com/users'
response = HTTParty.get(base_url)
emails = response.map { |user| user[‘email’] }
puts emails

HTTParty.get(base_url) directly returns a parsed JSON response.

No need for JSON.parse(response.body) because HTTParty does it automatically.

Run in your console:

base_url = 'https://fakestoreapi.com/users'
response = HTTParty.get(base_url)
emails = response.map { |user| user['email'] }
puts emails

3.Using Faraday (Flexible and Extensible)

Pros: Middleware support, easy to switch adapters (Net::HTTP, Typhoeus, Excon, etc.).
Cons: Requires additional setup.

🛰️ Install and Require Faraday 📡

gem install faraday

Then, require it:

require ‘faraday’
require ‘json’

🛰️ Initialize Faraday Connection📡

conn = Faraday.new(url: ‘https://fakestoreapi.com')

🛰️ Make the HTTP Request 📡

response = conn.get(‘/users’)

🛰️ Parse JSON Response 📡

data = JSON.parse(response.body)

🛰️ Extract Emails 📡

emails = data.map { |user| user[‘email’] }
puts emails

Run in your console:

require 'faraday'
conn = Faraday.new(url: 'https://fakestoreapi.com')
response = conn.get('/users')
data = JSON.parse(response.body)
emails = data.map { |user| user['email'] }
puts emails

Comparing Approaches

Press enter or click to view image in full size

Key Learnings

✅ Net::HTTP → Use when you want a built-in, dependency-free solution.
✅ HTTParty → Use when you want a quick and simple API request.
✅ Faraday → Use when you need more control, middlewares, or multiple adapters.

Related Post

Testing in the Console for New Finance Tracker — Rails App

--

--

J3
J3

Written by J3

Computer Engineer | AI & Data Systems | IoT & Embedded Systems | Product Creator — From Idea to Product | Founder @ Jungletronics & Kidstronics

Jungletronics
Jungletronics

Published in Jungletronics

Education & STEAM Learning by building. Practical explorations in AI, programming, electronics, robotics, IoT, and other STEAM topics — from ideas and experiments to working products. Follow and build with us.