Convert .json to .csv in ruby

前端 未结 7 1506
心在旅途
心在旅途 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 09:54

    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
    

提交回复
热议问题