CSV to JSON Ruby Script

CSV to JSON Ruby Script?

This is easy in ruby 1.9 where data is your csv data string

 require 'csv'
require 'json'

CSV.parse(data).to_json

Ruby Script for converting CSV to JSON

You are close to the solution, but let's reformat a little bit and simplify it

lines = CSV.open(ARGV[0],{:col_sep => "\|"}).readlines
# remove first entry of the lines array
keys = lines.shift

lines.each do |values|
# convert the line into a hash and transform string into int
hash=Hash[keys.zip(values.map{|val| is_int(val) ? val.to_i : val}) ]

# Write a file with the hash results
File.open("#{hash['NAME']}.json", "w") do |f|
f.write JSON.pretty_generate [hash]
end
end

Here you will open a new file for each line to be saved

CSV to JSON with line breaks with Ruby

If you want JSON, then the line breaks don't matter, but you should be outputting an array:

temp_json = Tempfile.new

File.open("#{temp_json.path}","w") do |f|
# note, map instead of each
f.write CSV.foreach("test.csv", headers: :first_row, liberal_parsing: true).map do |row|
data = {
EMAIL: row.fields[0],
vars: {
VALUE: row.fields[1]
}
}
end.to_json
end

Arrays will suck for really long files, as the whole thing has to be kept in memory and then formatted to JSON. For longer files or if you really want linebreaks for some reason, there is another format called JSONlines which is closer to CSV. You would write this like this:

temp_json = Tempfile.new

File.open("#{temp_json.path}","w") do |f|
CSV.foreach("test.csv", headers: :first_row, liberal_parsing: true ).each do |row|
data = {
EMAIL: row.fields[0],
vars: {
VALUE: row.fields[1]
}
}
f.puts(data.to_json)
end
end

Convert csv to json in ruby

You will need to map the modifiers yourself, as there is no built-in method to map hash values into an array from your logic:

JSON.pretty_generate(CSV.open('filename.csv', headers: true).map do |row|
modifier = {}
row.each do |k, v|
if k =~ /modifier(.)_(.*)$/
(modifier[$1] ||= {})[$2] = v
end
end
{ id: row['id'],
modifier: modifier.sort_by { |k, v| k }.map {|k, v| v }
}
end)

For the file*

id,modifier1_name,modifier1_price,modifier2_name,modifier2_price,modifier2_status
1,Small,10,Large,20,YYY
2,Small,20,Large,30,YYY

*I made some changes to the file you show, since it will not give you the required result - you state modifier2_price twice, for example

You will get:

[
{
"id": "1",
"modifier": [
{
"name": "Small",
"price": "10"
},
{

"name": "Large",
"price": "20",
"status": "YYY"
}
]
},
{
"id": "2",
"modifier": [
{
"name": "Small",
"price": "20"
},
{
"name": "Large",
"price": "30",
"status": "YYY"
}
]
}
]

generating json from csv file in Ruby

I think this code will help you ( I get it from http://jasonheppler.org/2014/07/12/parsing-csv-to-json/ ):

def readFromFile(fileName)
def is_int(str)
# Check if a string should be an integer
return !!(str =~ /^[-+]?[1-9]([0-9]*)?$/)
end

lines = CSV.open(filename).readlines
keys = lines.delete lines.first

File.open('some.json', "w") do |f|
data = lines.map do |values|
is_int(values) ? values.to_i : values.to_s
Hash[keys.zip(values)]
end
f.puts JSON.pretty_generate(data)
end
end

Convert .json to .csv in ruby

Try something like this:

require 'csv'
require 'json'

csv_string = CSV.generate do |csv|
JSON.parse(File.open("foo.json").read).each do |hash|
csv << hash.values
end
end

puts csv_string

Ruby: malformed CSV when converting to json

You have an extra space after the comma.

> CSV.parse '111, "Building fire. Excludes confined fires."'
=> CSV::MalformedCSVError: Illegal quoting in line 1.

> CSV.parse '111,"Building fire. Excludes confined fires."'
=> [["111", "Building fire. Excludes confined fires."]]

Comparing each line in CSV file to JSON hash on matching element to get unique matches only

You may not need two loops. You could do as below:
(Not tested as JSON structure was not shared in question)

results = @response["response"]["results"]

CSV.foreach("list.csv", :headers => true) do |row|
matching_entry = results.find { |data| row["IP Address"] == data["ip"]}
if matching_entry
puts "Match: " + row["IP Address"] + " = " + matching_entry["ID"]
else
puts "No Match Found for " + row["IP Address"]]
end
end


Related Topics



Leave a reply



Submit