Try it yourself with our free Json To Csv tool — runs entirely in your browser, no signup needed.

How to Convert JSON to CSV in Ruby

How to Convert JSON to CSV in Ruby

Converting JSON data to CSV is a common task in data processing and analysis. JSON (JavaScript Object Notation) is a lightweight data interchange format, while CSV (Comma Separated Values) is a widely-used format for tabular data. In Ruby, converting JSON to CSV can be achieved using the json and csv libraries. In this guide, we will walk through a step-by-step example of how to perform this conversion.

Quick Example

Here is a minimal example that converts a JSON string to a CSV string:

require 'json'
require 'csv'

json_string = '[{"name":"John","age":30},{"name":"Alice","age":25}]'
json_data = JSON.parse(json_string)

csv_string = CSV.generate do |csv|
  csv << ["Name", "Age"]
  json_data.each do |row|
    csv << [row["name"], row["age"]]
  end
end

puts csv_string

This code will output:

"Name","Age"
"John","30"
"Alice","25"

Step-by-Step Breakdown

Let's break down the code line by line:

  1. require 'json' and require 'csv': We require the json and csv libraries, which provide the necessary functionality for parsing JSON and generating CSV data.
  2. json_string = '[{"name":"John","age":30},{"name":"Alice","age":25}]': We define a sample JSON string containing two objects with name and age properties.
  3. json_data = JSON.parse(json_string): We parse the JSON string into a Ruby array of hashes using the JSON.parse method.
  4. csv_string = CSV.generate do |csv|: We use the CSV.generate method to create a new CSV string. The block passed to this method will be used to generate the CSV data.
  5. csv << ["Name", "Age"]: We add the header row to the CSV data using the << method.
  6. json_data.each do |row|: We iterate over the parsed JSON data using the each method.
  7. csv << [row["name"], row["age"]]: We add each row of data to the CSV string using the << method.

Handling Edge Cases

Here are a few common edge cases to consider when converting JSON to CSV:

Empty/Null Input

If the input JSON string is empty or null, we can add a simple check to handle this case:

json_string = ''
json_data = JSON.parse(json_string) rescue []

This code will parse an empty string as an empty array, avoiding a JSON::ParserError.

Invalid Input

If the input JSON string is invalid, we can use the JSON.parse method's symbolize_names option to raise an error:

begin
  json_data = JSON.parse(json_string, symbolize_names: true)
rescue JSON::ParserError => e
  puts "Invalid JSON: #{e.message}"
end

This code will raise a JSON::ParserError if the input JSON string is invalid.

Large Input

If the input JSON string is very large, we can use the JSON.parse method's stream option to parse the data in chunks:

json_data = []
File.open('large_json_file.json') do |file|
  JSON.parse(file, stream: true).each do |row|
    json_data << row
  end
end

This code will parse a large JSON file in chunks, avoiding memory issues.

Unicode/Special Characters

If the input JSON string contains Unicode or special characters, we can use the CSV.generate method's col_sep option to specify a custom column separator:

csv_string = CSV.generate(col_sep: "\t") do |csv|
  # ...
end

This code will use a tab character (\t) as the column separator, which can help with Unicode and special character issues.

Common Mistakes

Here are a few common mistakes to avoid when converting JSON to CSV:

Mistake 1: Forgetting to require the json and csv libraries

# Wrong
json_string = '[{"name":"John","age":30},{"name":"Alice","age":25}]'
json_data = JSON.parse(json_string)

# Correct
require 'json'
require 'csv'
json_string = '[{"name":"John","age":30},{"name":"Alice","age":25}]'
json_data = JSON.parse(json_string)

Mistake 2: Not handling invalid input

# Wrong
json_string = '[{"name":"John","age":30},{"name":"Alice","age":25}]'
json_data = JSON.parse(json_string)

# Correct
begin
  json_data = JSON.parse(json_string)
rescue JSON::ParserError => e
  puts "Invalid JSON: #{e.message}"
end

Mistake 3: Not handling large input

# Wrong
json_string = File.read('large_json_file.json')
json_data = JSON.parse(json_string)

# Correct
json_data = []
File.open('large_json_file.json') do |file|
  JSON.parse(file, stream: true).each do |row|
    json_data << row
  end
end

Performance Tips

Here are a few performance tips to keep in mind when converting JSON to CSV:

  • Use the JSON.parse method's stream option to parse large JSON files in chunks.
  • Use the CSV.generate method's col_sep option to specify a custom column separator.
  • Avoid using the JSON.parse method's symbolize_names option unless necessary, as it can slow down parsing.

FAQ

Q: What is the best way to handle invalid input when converting JSON to CSV?

A: Use the JSON.parse method's rescue block to catch and handle JSON::ParserError exceptions.

Q: How can I improve performance when converting large JSON files to CSV?

A: Use the JSON.parse method's stream option to parse the data in chunks.

Q: What is the default column separator used by the CSV.generate method?

A: The default column separator is a comma (,).

Q: Can I use the CSV.generate method to generate CSV data with Unicode characters?

A: Yes, but you may need to specify a custom column separator using the col_sep option.

Q: How can I handle empty or null input when converting JSON to CSV?

A: Use the JSON.parse method's rescue block to catch and handle JSON::ParserError exceptions, and add a simple check to handle empty or null input.

AI agent tools available. The CodeTidy MCP Server gives Claude, Cursor, and other AI agents access to 60+ developer tools. One command: npx @codetidy/mcp