Convert YAML to JSON in Ruby — Built-In, No Gems Required
- Ruby's standard library includes both yaml and json — no gems needed for basic conversion.
- YAML.safe_load parses YAML to a Ruby hash/array; JSON.pretty_generate outputs formatted JSON.
- One-liner: ruby -ryaml -rjson -e "puts JSON.pretty_generate(YAML.safe_load(STDIN.read))"
- Use YAML.safe_load (not YAML.load) to prevent arbitrary Ruby code execution from untrusted YAML.
Table of Contents
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:
- YAML.safe_load — parses YAML data types only (strings, numbers, arrays, hashes, booleans, null). Cannot execute Ruby code.
- YAML.load — can deserialize arbitrary Ruby objects. A malicious YAML file can execute arbitrary Ruby code when parsed with
YAML.load.
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 ConverterFrequently 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.

