Blog
Wild & Free Tools

Convert YAML to JSON in Ruby — Built-In, No Gems Required

Last updated: January 2026 4 min read
Quick Answer

Table of Contents

  1. One-Liner YAML to JSON
  2. Ruby Script for YAML to JSON
  3. YAML.safe_load vs YAML.load
  4. Use in a Rails or Ruby App
  5. Frequently Asked Questions

Ruby ships with both a YAML parser and a JSON encoder in its standard library. Converting YAML to JSON in Ruby requires no gems — just two requires and a one-liner. This is one of the fastest scripting approaches available, especially on macOS and Linux where Ruby is pre-installed.

One-Liner — Convert YAML to JSON from the Terminal

Ruby's -r flag loads libraries, -e runs inline code. This one-liner reads YAML from stdin and prints JSON:

ruby -ryaml -rjson -e "puts JSON.pretty_generate(YAML.safe_load(STDIN.read))"

Pipe a file into it:

cat config.yaml | ruby -ryaml -rjson -e "puts JSON.pretty_generate(YAML.safe_load(STDIN.read))"

Or pass a file directly using $stdin redirect:

ruby -ryaml -rjson -e "puts JSON.pretty_generate(YAML.safe_load(STDIN.read))" < config.yaml

Save to file:

ruby -ryaml -rjson -e "puts JSON.pretty_generate(YAML.safe_load(STDIN.read))" < config.yaml > config.json

This works on any system with Ruby installed — macOS (pre-installed), most Linux distributions, and Windows with RubyInstaller.

Ruby Script — Convert a YAML File to JSON

A standalone script that accepts a filename as an argument:

require 'yaml'
require 'json'

filename = ARGV[0] || raise("Usage: ruby yaml2json.rb ")
yaml_data = File.read(filename)
parsed = YAML.safe_load(yaml_data)
puts JSON.pretty_generate(parsed)

Run it:

ruby yaml2json.rb config.yaml

Or save the output:

ruby yaml2json.rb config.yaml > config.json

To process multiple files in a directory:

require 'yaml'
require 'json'

Dir.glob('*.yaml').each do |file|
  parsed = YAML.safe_load(File.read(file))
  json_file = file.sub('.yaml', '.json')
  File.write(json_file, JSON.pretty_generate(parsed))
  puts "Converted: #{file} -> #{json_file}"
end
Sell Custom Apparel — We Handle Printing & Free Shipping

Always Use YAML.safe_load — Not YAML.load

Ruby's YAML library has two parse methods:

Use YAML.safe_load for all config file parsing, user input, and any YAML from an untrusted source. Only use YAML.load if you are round-tripping Ruby-serialized objects you fully control.

Ruby 3.1+ added an additional safety layer — YAML.load defaults to safe mode and requires an explicit permitted_classes argument to deserialize objects. YAML.safe_load remains the recommended approach regardless.

Using YAML to JSON in a Rails or Ruby Application

In a Rails app or any Ruby project, the same standard library approach works:

require 'yaml'
require 'json'

def yaml_to_json(yaml_string)
  data = YAML.safe_load(yaml_string)
  JSON.generate(data)
end

# Pretty-printed version
def yaml_to_json_pretty(yaml_string)
  data = YAML.safe_load(yaml_string)
  JSON.pretty_generate(data)
end

Rails applications already have both libraries available. No additional requires are needed in controllers or service objects — Rails loads them automatically.

For quick one-off conversions during development (e.g. checking how a YAML config parses), paste the content into the free browser-based converter instead of writing a script.

No Ruby? Use the Browser Converter

Paste your YAML and click Convert. Instant JSON output — no Ruby, no install, no terminal needed.

Open Free YAML to JSON Converter

Frequently Asked Questions

How do I convert YAML to JSON in Ruby?

Ruby's standard library handles both. Require yaml and json, then: parsed = YAML.safe_load(yaml_string); json_output = JSON.pretty_generate(parsed). No gems needed. As a one-liner: ruby -ryaml -rjson -e "puts JSON.pretty_generate(YAML.safe_load(STDIN.read))" < file.yaml

Does Ruby have a built-in YAML parser?

Yes. Ruby's standard library includes the yaml module (Psych, a YAML 1.1 parser). It is available without installing any gems. Use require 'yaml' or require 'psych' to access it.

What is the difference between YAML.safe_load and YAML.load in Ruby?

YAML.safe_load only parses standard YAML data types — strings, numbers, arrays, hashes, booleans, null. YAML.load can deserialize arbitrary Ruby objects, which allows code execution from untrusted YAML input. Always use YAML.safe_load for config files and any external input.

Can I convert YAML to JSON on macOS without installing anything?

Yes. macOS ships with Ruby pre-installed. Run: ruby -ryaml -rjson -e "puts JSON.pretty_generate(YAML.safe_load(STDIN.read))" < config.yaml. No Homebrew, no gems, no setup required.

Alicia Grant
Alicia Grant Frontend Engineer

Alicia leads image and PDF tool development at WildandFree, specializing in high-performance client-side browser tools.

More articles by Alicia →
Launch Your Own Clothing Brand — No Inventory, No Risk