Choosing a Format
Trade-offs.
Choosing a Format is a free Ruby Academy lesson on CoddyKit. This is lesson 4 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, and your progress syncs across the web and the CoddyKit app. The Ruby Academy course includes 4 lessons in total.
Three Formats, Three Jobs
You now know JSON, CSV, and YAML. Each shines in different situations. Picking the right one keeps your data easy to produce, store, and consume.
- JSON for APIs and web
- CSV for tabular/spreadsheet data
- YAML for configuration
puts ['JSON', 'CSV', 'YAML'].join(', ')JSON Strengths
JSON is compact, universally supported, and maps to nested Hashes and Arrays. It is the de facto standard for web APIs and data interchange between services.
require 'json'
puts({ id: 1, items: [1, 2] }.to_json)JSON Weaknesses
JSON has no comments and is verbose for humans to hand-edit. Trailing commas and single quotes are invalid, which trips people up in config files.
require 'json'
begin
JSON.parse('{"a":1,}')
rescue JSON::ParserError
puts 'trailing comma not allowed'
endCSV Strengths
CSV is perfect for flat, tabular data: rows and columns. Spreadsheets, exports, and bulk imports all speak CSV, and it is extremely compact for large tables.
require 'csv'
puts(CSV.generate { |c| c << ['a', 'b']; c << [1, 2] })CSV Weaknesses
CSV cannot represent nested or hierarchical data. It has no standard type system, so everything is a string until you convert it, and dialects (separators, quoting) vary.
require 'csv'
row = CSV.parse_line('5,true')
puts(row[0].class)
puts(row[1].class)YAML Strengths
YAML is the most human-readable, supports comments, and handles nesting cleanly. That makes it ideal for configuration files developers edit by hand.
require 'yaml'
text = "# app config\nport: 8081"
puts(YAML.load(text)['port'])YAML Weaknesses
YAML's indentation rules are error-prone (a stray space breaks it), and unsafe loading can be a security risk. It is also slower to parse than JSON.
require 'yaml'
cfg = YAML.safe_load("port: 8081")
puts(cfg['port'])Nesting Comparison
JSON and YAML both represent the same nested structure naturally; CSV cannot.
require 'json'
require 'yaml'
data = { 'user' => { 'name' => 'Ada', 'roles' => ['admin'] } }
puts(data.to_json)
puts(data.to_yaml)Readability vs Compactness
Trade-off: YAML is most readable, JSON is balanced, CSV is most compact for tables. Choose based on whether humans or machines are the main consumer.
require 'json'
require 'yaml'
h = { 'a' => 1, 'b' => 2 }
puts("JSON len: #{h.to_json.length}")
puts("YAML len: #{h.to_yaml.length}")A Decision Guide
Quick rules:
- Talking to a web API? JSON
- Exporting rows for a spreadsheet? CSV
- App settings a human edits? YAML
When in doubt, JSON is the safest interchange default.
choice = { api: 'JSON', table: 'CSV', config: 'YAML' }
puts(choice[:config])Converting Between Formats
Because all three parse into the same Ruby Hash/Array structures, converting is straightforward: parse one, dump another.
require 'json'
require 'yaml'
data = JSON.parse('{"port":8081,"name":"app"}')
puts(data.to_yaml)Quick Check
Which format is the best default choice for a configuration file that developers edit by hand and want comments in?
Recap: Choosing a Format
You can now pick the right tool:
- JSON: APIs, interchange, balanced
- CSV: flat tables, spreadsheets, compact
- YAML: config, readable, comments, nesting
- All parse to the same Ruby structures, so conversion is easy
require 'yaml'
require 'json'
puts(YAML.load('a: 1').to_json)Learn Ruby with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 29
- Lessons
- 109
Frequently Asked Questions
Is the “Choosing a Format” lesson free?
Yes — any 3 lessons of the Ruby Academy learning path, including “Choosing a Format”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Ruby Academy course includes 4 lessons in total.
What will I learn in “Choosing a Format”?
Trade-offs. You practise Ruby Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Ruby Academy?
No prior experience is required. Ruby Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.
How long does the “Choosing a Format” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Ruby Academy lesson?
Yes. Every Ruby Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Reading and Writing JSON
- Working with CSV
- YAML Configuration
- Choosing a Format