Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Data extraction in Ruby starts by identifying the input format. Use Ruby’s JSON library for JSON, YAML/Psych for YAML, Nokogiri for HTML or XML, and strings plus regular expressions only for genuinely line-oriented text. The parser determines what structure you can trust, how malformed input is handled, and whether you can process a large document incrementally.

This guide uses Ruby 4.0 as the reference point. Match the Ruby documentation release to the runtime actually deployed, because standard-library behavior and available APIs can differ between versions and implementations.

Choose the parser from the input format

Input Ruby path Best starting strategy
Line-oriented text String methods and regular expressions Read one record at a time when the format has clear delimiters.
JSON Ruby JSON library Decode into hashes and arrays, then validate required keys.
YAML YAML/Psych Parse only when YAML is expected and treat input as untrusted.
HTML or XML Nokogiri Use CSS or XPath for targeted queries; choose DOM or streaming modes based on size.

Do not use Nokogiri to parse a JSON file. A question such as “I’m trying to scrap this json file with nokogiri” combines two incompatible formats: Nokogiri queries markup trees, while JSON must be decoded by the JSON library.

Prepare Ruby and the dependencies

Ruby includes standard-library support for JSON and YAML/Psych. Nokogiri is an external gem for HTML and XML. Keep your runtime and dependency versions explicit in the application so development and production use the same parser behavior.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall
Elebase USB to USB C Adapter for iPhone 18 Pro Max,USBC Car Charger Adapter
  • Read Before You Buy — No Video Output: These adapters support charging and USB 2.0 data transfer, but cannot transmit video signals. Except for standard USB webcams (which use USB data only), they are not compatible with HDMI/DisplayPort cables, video-capable USB-C hubs, or docking stations with video output.
  • Convert USB-A Ports to USB-C: Designed to connect USB-C earphones, cables, flash drives, card readers, and other USB-C accessories to standard USB-A ports. Plug-and-play with no drivers or software required.
  • Aluminum Alloy Housing: Built with a sturdy aluminum alloy shell that aids in heat dissipation and protects against daily wear and scratches. Designed to maintain a stable and secure connection.
  • Compact & Travel-Friendly: The ultra-compact design allows the adapter to stay plugged into your device without blocking adjacent ports or adding bulk, reducing wear and tear on your original USB ports.
  • 12-Month Warranty: Backed by a 12-month manufacturer warranty for peace of mind. Designed to meet strict quality control standards for reliable everyday performance.
# Gemfile
gem "nokogiri"

# shell
bundle install

For a one-off script, install Nokogiri with gem install nokogiri, then require the libraries you use:

require "json"
require "yaml"
require "nokogiri"

Extract values from JSON

JSON extraction is a decode-and-navigate operation. Parse the complete document when it is a manageable payload, then inspect the resulting hashes and arrays. Check types and required keys before using values from an external response.

require "json"

json_text = File.read("orders.json", encoding: "UTF-8")
data = JSON.parse(json_text)

orders = data.fetch("orders", [])
rows = orders.filter_map do |order|
  id = order["id"]
  customer = order.dig("customer", "email")
  total = order["total"]
  next unless id && customer && total

  { id: id, email: customer, total: total }
end

puts JSON.pretty_generate(rows)

Handle JSON failures deliberately

JSON.parse raises when the text is not valid JSON. Catch the parsing error at the boundary where you read the response, log enough context to diagnose the source without logging secrets, and return a useful application error.

begin
  data = JSON.parse(json_text)
rescue JSON::ParserError => e
  warn "Invalid JSON: #{e.message}"
  exit 1
end

Do not silently convert malformed input to an empty hash: that can turn an upstream outage into apparently valid, empty data.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #2
Anker USB-C Hub, 5-in-1 USB Hub for Laptops, 4K HDMI Multiport Adapter
  • 5-in-1 USB-C Hub: Experience comprehensive connectivity featuring a Power Delivery input, two USB-A 2.0 ports, a USB-A 3.0 port, and an HDMI port. (Note: The USB-C power delivery input port is only for connecting an external wall charger to power your laptop and cannot power peripheral devices.)
  • 90W Pass-Through Charging: Achieve optimal charging with 90W pass-through power to your laptop, supported by a total input of 100W, with the hub reserving 10W for operational efficiency. (Note: Wall charger not included.)
  • Quick Data Transfers: Accelerate your productivity with rapid data transfers using a high-speed 5Gbps USB 3.0 port and two 480Mbps USB 2.0 ports.
  • 4K HDMI Display: Enhance your visual experience with a hub capable of delivering 4K resolution at 30Hz in both mirror and extend modes. Please note that this hub is compatible with MacBook (macOS 12 and newer), Windows 10 and 11, ChromeOS, and laptops equipped with DP Alt Mode and Power Delivery. Note: This device is not compatible with Linux.
  • What You Get: Anker USB-C Hub (5-in-1, 4K HDMI), welcome guide, 18-month warranty, and our friendly customer service.

Extract values from YAML

YAML can represent richer structures than JSON, so identify YAML explicitly and decide what tags and types your application permits. YAML/Psych provides parsing and emission facilities in Ruby’s standard library. For configuration or user-supplied files, prefer a safe loading policy and avoid evaluating arbitrary application objects.

require "yaml"

text = File.read("settings.yml", encoding: "UTF-8")
settings = YAML.safe_load(text, aliases: false) || {}

host = settings.fetch("host")
port = Integer(settings.fetch("port", 443))
features = Array(settings["features"])

puts "#{host}:#{port}"
puts features.join(", ")

YAML aliases, permitted classes, and keyword arguments can vary with the Ruby/Psych release. Check the documentation for the Ruby version you deploy and specify permitted behavior rather than relying on defaults.

Extract text with strings and regular expressions

The official Ruby FAQ notes that Ruby is good at text processing and demonstrates parsing records line by line with regular expressions. That approach is appropriate for a bounded, documented text format such as one record per line with a fixed delimiter.

records = []

File.foreach("events.log", encoding: "UTF-8") do |line|
  next if line.strip.empty?

  match = /A(?<timestamp>S+)s+(?<level>INFO|WARN|ERROR)s+(?<message>.*)z/.match(line.chomp)
  next unless match

  records << match.named_captures
end

p records

Regular expressions do not understand nested markup, quoted delimiters, escaped JSON strings, or arbitrary line breaks. Once the format has structure, switch to its parser instead of adding increasingly fragile patterns.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #3
Sale
Anker USB C Hub, 7in1 Multi-Port USB Adapter, 4K@60Hz USBC to HDMI Splitter
  • Sleek 7-in-1 USB-C Hub: Features an HDMI port, two USB-A 3.0 ports, and a USB-C data port, each providing 5Gbps transfer speeds. It also includes a USB-C PD input port for charging up to 100W and dual SD and TF card slots, all in a compact design.
  • Flawless 4K@60Hz Video with HDMI: Delivers exceptional clarity and smoothness with its 4K@60Hz HDMI port, making it ideal for high-definition presentations and entertainment. (Note: Only the HDMI port supports video projection; the USB-C port is for data transfer only.)
  • Double Up on Efficiency: The two USB-A 3.0 ports and a USB-C port support a fast 5Gbps data rate, significantly boosting your transfer speeds and improving productivity.
  • Fast and Reliable 85W Charging: Offers high-capacity, speedy charging for laptops up to 85W, so you spend less time tethered to an outlet and more time being productive.
  • What You Get: Anker USB-C Hub (7-in-1), welcome guide, 18-month warranty, and our friendly customer service.

Extract HTML and XML with Nokogiri

Nokogiri supports DOM parsing, SAX parsing, and push parsing, along with XPath 1.0 and CSS3 selector queries. DOM is convenient when you need to query a document repeatedly. SAX or push parsing can reduce memory use for large streams but requires event-oriented code. Nokogiri documents DOM parsers for XML, HTML4, and HTML5; SAX and push modes cover XML and HTML4, so confirm the mode and markup type you need.

Query HTML with CSS selectors

require "nokogiri"

html = File.read("catalog.html", encoding: "UTF-8")
doc = Nokogiri::HTML5.parse(html)

products = doc.css("article.product").filter_map do |node|
  name = node.at_css("h2")&.text&.strip
  price = node.at_css(".price")&.text&.strip
  next unless name && price

  { name: name, price: price }
end

products.each { |product| puts product.inspect }

Use at_css when one matching node is expected and css when you need all matches. Calling text returns descendant text; strip and normalize it at the extraction boundary so later code receives predictable values.

Query XML with XPath

require "nokogiri"

xml = File.read("feed.xml", encoding: "UTF-8")
doc = Nokogiri::XML(xml)

doc.xpath("//item").each do |item|
  title = item.at_xpath("string(title)").to_s.strip
  link = item.at_xpath("string(link)").to_s.strip
  puts "#{title}t#{link}"
end

XPath is useful when selection depends on ancestry, attributes, or conditions that are awkward to express in a CSS selector. Namespaces in XML require namespace-aware XPath expressions; inspect the document’s namespace declarations before writing selectors.

Choose DOM, SAX, or push parsing

  • DOM: easiest for random access, multiple queries, and small-to-medium documents; it keeps a tree in memory.
  • SAX: handles XML or HTML4 as a sequence of parser events; useful when you can emit records as elements close.
  • Push parsing: lets your code feed chunks to the parser, which suits streaming input and custom I/O.

There is no universally best mode. Base the decision on document size, whether you need random access, and whether your extraction logic can operate incrementally.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #4
Sale
UGREEN USB to USB C Adapter Combo 4-Pack, 10Gbps USB C Converter Space Gray
  • Dual Converters, Infinite Potential:Includes 2× USB C male to USB A female adapters and 2× USB A male to USB C female adapters. Perfect for a wide range of uses—tablets with Bluetooth keyboards, expand USB ports on macbook, and more. Two different converters for all your daily needs
  • Next-Level 10Gbps & 3A Charging: No more slow 480Mbps, this usb to usb c adapter has a transfer speed of up to 10Gbps, allowing you to do more transferring in less time. This usb adapter fits both USB A and USB C charger, supporting up to 3A fast charging
  • Upgraded Exquisite Craftsmanship: With an aluminum alloy housing and metal connector, the usbc to usb adapter is extremely durable and sturdy. Rigorously tested to withstand more than 10,000 times of plugging and unplugging, ensuring long-lasting performance
  • Broad Compatible: The usb c to usb adapter widely supports all USB C/ USB A devices like laptops, tablets, cellphones, car chargers, and phone chargers. Such as compatible with MacBook Pro/Air 2023/2022, Thunderbolt 4/3 Devices,Apple MagSafe Watch 9/8/7/SE/Ultra, iPad Pro 2022/2021, Samsung Galaxy S23/S20/S10, and iPhone 17/16/15 Pro. Plug and play
  • Please Note: To reach 10Gbps speed, keep the cable under 3.3 ft. For USB A Male to USB C adapters, try flipping the USB C connector. USB C Male to USB A adapters support bidirectional 10Gbps transfer within 3.3 ft

Security, encoding, and parser differences

Nokogiri describes its guiding principle as being secure by default by treating documents as untrusted. That principle does not make an application automatically secure: validate extracted values, restrict network access in surrounding code, and avoid inserting untrusted HTML into a browser without output escaping.

Encoding detection is not perfectly accurate. Nokogiri’s documentation describes input as a stream of bytes, notes that 100% accurate detection is impossible, and recommends explicitly setting the encoding when it is known or consequential.

bytes = File.binread("legacy.html")
doc = Nokogiri::HTML5.parse(bytes, nil, "ISO-8859-1")
name = doc.at_css(".name")&.text&.encode("UTF-8", invalid: :replace, undef: :replace)

Nokogiri relies on native parsers and documents implementation differences, including differences between CRuby and JRuby. Test the exact Ruby implementation, Nokogiri version, parser mode, and representative malformed inputs used in production.

Build an extraction pipeline that survives bad input

  1. Identify the format. Confirm the content type, encoding, and whether the producer can change the schema.
  2. Parse once at the boundary. Convert bytes into a parser object or Ruby data structure and handle parse errors there.
  3. Select narrowly. Prefer stable IDs, semantic elements, and explicit JSON keys over positional assumptions.
  4. Normalize values. Strip whitespace, convert numeric fields deliberately, and normalize encoding before persistence.
  5. Validate required fields. Reject or quarantine records that lack identifiers or have impossible values.
  6. Measure memory and latency. Move from DOM to SAX or push parsing when document size makes a tree impractical.
  7. Test fixtures and failures. Include empty documents, malformed markup, missing keys, duplicate elements, unusual encodings, and schema changes.

Common errors and fixes

Symptom Likely cause Fix
JSON::ParserError The response is HTML, truncated, or invalid JSON. Log the content type and a bounded prefix, verify the endpoint, then parse only confirmed JSON.
Nokogiri returns no nodes The selector does not match the actual tree, or content is rendered after initial HTML. Inspect saved source, test a simpler selector, and account for namespaces or client-side rendering.
Garbled accented text Source encoding was guessed incorrectly. Read bytes, identify the declared/source encoding, and pass it explicitly when parsing.
Memory grows on large files DOM retains the entire tree. Use SAX or push parsing and emit records incrementally.
Works on one Ruby implementation only Native parser or runtime differences. Pin versions and run the same fixture suite on every supported implementation.
YAML loads unexpected objects Untrusted YAML was loaded with overly broad permissions. Use a safe loading policy and explicitly permit only required classes or aliases.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Or skip the browser setup: capture a page with ScreenshotNeo

If your extraction workflow begins with obtaining a stable visual snapshot rather than parsing the page’s DOM, ScreenshotNeo provides a website screenshot API. It accepts consent banners before capture and removes more than 60 known consent platforms, newsletter popups, and chat widgets; each step can be disabled. Bot checks, CAPTCHAs, blank pages, timeouts, failed loads, and cache hits are not billed, and response headers identify the page verdict and billing result. Its MCP server exposes take_screenshot, get_page_info, and capture_pdf to Claude, Cursor, and other MCP clients.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use the ScreenshotNeo API documentation for all options, including full-page lazy-image loading, CSS-selector element capture, device presets, custom CSS and JavaScript, waits, request blocking, cookies, headers, geolocation, PDF output, signed links, asynchronous jobs, bulk capture, caching, and usage reporting.

Best Value
Anker USB C Hub, 5-in-1 USBC to HDMI Splitter with 4K Display
  • 5-in-1 Connectivity: Equipped with a 4K HDMI port, a 5 Gbps USB-C data port, two 5 Gbps USB-A ports, and a USB C 100W PD-IN port. Note: The USB C 100W PD-IN port supports only charging and does not support data transfer devices such as headphones or speakers.
  • Powerful Pass-Through Charging: Supports up to 85W pass-through charging so you can power up your laptop while you use the hub. Note: Pass-through charging requires a charger (not included). Note: To achieve full power for iPad, we recommend using a 45W wall charger.
  • Transfer Files in Seconds: Move files to and from your laptop at speeds of up to 5 Gbps via the USB-C and USB-A data ports. Note: The USB C 5Gbps Data port does not support video output.
  • HD Display: Connect to the HDMI port to stream or mirror content to an external monitor in resolutions of up to 4K@30Hz. Note: The USB-C ports do not support video output.
  • What You Get: Anker 332 USB-C Hub (5-in-1), welcome guide, our worry-free 18-month warranty, and friendly customer service.
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp
import requests
r = requests.get("https://api.screenshotneo.com/v1/shot", params={"access_key": "YOUR_API_KEY", "url": "https://stripe.com"}, timeout=90)
r.raise_for_status()
open("shot.webp", "wb").write(r.content)
const q = new URLSearchParams({ access_key: 'YOUR_API_KEY', url: 'https://stripe.com' });
const res = await fetch(`https://api.screenshotneo.com/v1/shot?${q}`);
if (!res.ok) throw new Error(`HTTP ${res.status}`);
const bytes = new Uint8Array(await res.arrayBuffer());

Ruby can call the same endpoint with Net::HTTP:

require "net/http"
require "uri"

uri = URI("https://api.screenshotneo.com/v1/shot")
uri.query = URI.encode_www_form(access_key: ENV.fetch("SCREENSHOTNEO_KEY"), url: "https://stripe.com")
response = Net::HTTP.get_response(uri)
raise "Screenshot failed: #{response.code}" unless response.is_a?(Net::HTTPSuccess)
File.binwrite("shot.webp", response.body)

The Free plan includes 1,000 screenshots per month with no card. Paid plans start at $5 for 3,000 shots; every feature is available on every plan. Sign up for ScreenshotNeo free.

Cost, reliability, and operational choices

  • Parsing local JSON, YAML, HTML, or XML avoids network cost; the dominant limits are CPU, memory, and input size.
  • Cache source documents when repeatability matters, but retain the original bytes when you may need to diagnose encoding or parser changes.
  • For remote pages, separate fetching from extraction so HTTP retries, rate limits, and parser failures can be observed independently.
  • Record parser, Ruby, and native-library versions with extraction jobs. A selector that succeeds today can fail when a producer changes its markup.
  • Use bounded timeouts and maximum document sizes for untrusted or remote input, and quarantine records that fail validation rather than silently dropping them.

Frequently Asked Questions

Can Nokogiri parse a PDF?

Nokogiri is documented for HTML and XML. PDF extraction requires a PDF-specific tool, which is outside this HTML/XML workflow.

Should I use CSS or XPath?

Use whichever expresses the selection clearly: CSS is concise for classes and elements, while XPath is useful for attributes, ancestry, namespaces, and conditions.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Which Ruby version should I target?

Target the Ruby release deployed by your application and read that release’s standard-library and Nokogiri documentation; do not assume behavior is identical across versions or Ruby implementations.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.