Convert .json to .csv in ruby

前端 未结 7 1521
心在旅途
心在旅途 2020-12-24 09:42

I want to convert .json file into .csv file using ruby. Pleases help me to do this.

Also propose any tool to achieve this.

7条回答
  •  北荒
    北荒 (楼主)
    2020-12-24 10:01

    To actually write to file...

    require 'csv'
    require 'json'
    
    CSV.open("your_csv.csv", "w") do |csv| #open new file for write
      JSON.parse(File.open("your_json.json").read).each do |hash| #open json to parse
        csv << hash.values #write value to file
      end
    end
    

提交回复
热议问题