Advertisement
Guest User

Untitled

a guest
Aug 24th, 2017
449
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.34 KB | None | 0 0
  1. require 'csv'
  2. require 'json'
  3.  
  4. csv_string = CSV.generate do |csv|
  5. JSON.parse(File.open("foo.json").read).each do |hash|
  6. csv << hash.values
  7. end
  8. end
  9.  
  10. puts csv_string
  11.  
  12. require 'csv'
  13. require 'json'
  14.  
  15. CSV.open("your_csv.csv", "w") do |csv| #open new file for write
  16. JSON.parse(File.open("your_json.json").read).each do |hash| #open json to parse
  17. csv << hash.values #write value to file
  18. end
  19. end
  20.  
  21. json2csv convert data/sample.json
  22.  
  23. cat data/sample.json
  24.  
  25. {
  26. "12345": {
  27. "Firstname": "Joe",
  28. "Lastname": "Doe",
  29. "Address": {
  30. "Street": "#2140 Taylor Street, 94133",
  31. "City": "San Francisco",
  32. "Details": {
  33. "note": "Pool available"
  34. }
  35. }
  36. },
  37.  
  38. "45678": {
  39. "Firstname": "Jack",
  40. "Lastname": "Plumber",
  41. "Address": {
  42. "Street": "#111 Sutter St, 94104",
  43. "City": "San Francisco",
  44. "Details": {
  45. "note": "Korean Deli near to main entrance"
  46. }
  47. }
  48. }
  49.  
  50. cat data/sample.json.csv
  51.  
  52. id,Firstname,Lastname,Address.Street,Address.City,Address.Details.note
  53. 12345,Joe,Doe,"#2140 Taylor Street, 94133",San Francisco,Pool available
  54. 45678,Jack,Plumber,"#111 Sutter St, 94104",San Francisco,Korean Deli near to main entrance
  55.  
  56. # utils.rb
  57. require "csv"
  58. module Utils
  59. def self.array_of_hashes_to_csv(array_of_hashes)
  60. CSV.generate do |csv|
  61. csv << array_of_hashes.first.keys
  62. array_of_hashes.each { |hash| csv << hash.values }
  63. end
  64. end
  65. end
  66.  
  67. # utils_test.rb
  68. class UtilsTest < MiniTest::Unit::TestCase
  69. def test_array_of_hashes_to_csv
  70. array_of_hashes = [
  71. { :key1 => "value1", :key2 => "value2" },
  72. { :key1 => "value3", :key2 => "value4" }
  73. ]
  74. expected_result = "key1,key2nvalue1,value2nvalue3,value4n"
  75. assert_equal(expected_result, Utils.array_of_hashes_to_csv(array_of_hashes))
  76. end
  77. end
  78.  
  79. #json-to-csv.rb
  80. require 'json'
  81. require 'csv'
  82.  
  83. file = File.read('./input.json')
  84. hash = JSON.parse(file)
  85.  
  86. CSV.open('./output.csv', 'w') do |csv|
  87. headers = hash.first.keys
  88. csv << headers
  89.  
  90. hash.each do |item|
  91. values = item.values
  92. printable_values = Array.new
  93. values.each do |value|
  94.  
  95. printable_values << value.to_s.gsub(/[|]/,'').gsub(/"/,''')
  96.  
  97. end
  98.  
  99. csv << printable_values
  100.  
  101. end
  102.  
  103. end
  104.  
  105. require 'json_converter'
  106. json_converter= JsonConverter.new
  107.  
  108. # Assume json is a valid JSON string or object
  109. csv = json_converter.generate_csv json
  110.  
  111. # Attempt to identify what a "row" should look like
  112. def array_from(json)
  113. queue, next_item = [], json
  114. while !next_item.nil?
  115.  
  116. return next_item if next_item.is_a? Array
  117.  
  118. if next_item.is_a? Hash
  119. next_item.each do |k, v|
  120. queue.push next_item[k]
  121. end
  122. end
  123.  
  124. next_item = queue.shift
  125. end
  126.  
  127. return [json]
  128. end
  129.  
  130. # The path argument is used to construct header columns for nested elements
  131. def flatten(object, path='')
  132. scalars = [String, Integer, Fixnum, FalseClass, TrueClass]
  133. columns = {}
  134.  
  135. if [Hash, Array].include? object.class
  136. object.each do |k, v|
  137. new_columns = flatten(v, "#{path}#{k}/") if object.class == Hash
  138. new_columns = flatten(k, "#{path}#{k}/") if object.class == Array
  139. columns = columns.merge new_columns
  140. end
  141.  
  142. return columns
  143. elsif scalars.include? object.class
  144. # Remove trailing slash from path
  145. end_path = path[0, path.length - 1]
  146. columns[end_path] = object
  147. return columns
  148. else
  149. return {}
  150. end
  151. end
  152.  
  153. # Recursively convert all nil values of a hash to empty strings
  154. def nils_to_strings(hash)
  155. hash.each_with_object({}) do |(k,v), object|
  156. case v
  157. when Hash
  158. object[k] = nils_to_strings v
  159. when nil
  160. object[k] = ''
  161. else
  162. object[k] = v
  163. end
  164. end
  165. end
  166.  
  167. json = JSON.parse(File.open('in.json').read)
  168. in_array = array_from json
  169. in_array.map! { |x| nils_to_strings x }
  170.  
  171. out_array = []
  172. in_array.each do |row|
  173. out_array[out_array.length] = flatten row
  174. end
  175.  
  176. headers_written = false
  177. CSV.open('out.csv', 'w') do |csv|
  178. out_array.each do |row|
  179. csv << row.keys && headers_written = true if headers_written === false
  180. csv << row.values
  181. end
  182. end
  183.  
  184. {
  185. "Forms": [
  186. {
  187. "Form": {
  188. "id": "x",
  189. "version_id": "x",
  190. "name": "x",
  191. "category": "",
  192. "subcategory": null,
  193. "is_template": null,
  194. "moderation_status": "x",
  195. "display_status": "x",
  196. "use_ssl": "x",
  197. "modified": "x",
  198. "Aggregate_metadata": {
  199. "id": "x",
  200. "response_count": "x",
  201. "submitted_count": "x",
  202. "saved_count": "x",
  203. "unread_count": "x",
  204. "dropout_rate": "x",
  205. "average_completion_time": null,
  206. "is_uptodate": "x"
  207. }
  208. },
  209. "User": {
  210. "username": "somedude@example.com"
  211. }
  212. },
  213. {
  214. "Form": {
  215. "id": "x",
  216. "version_id": "x",
  217. "name": "x",
  218. "category": "",
  219. "subcategory": null,
  220. "is_template": null,
  221. "moderation_status": "x",
  222. "display_status": "x",
  223. "use_ssl": "x",
  224. "modified": "x",
  225. "Aggregate_metadata": {
  226. "id": "x",
  227. "response_count": "x",
  228. "submitted_count": "x",
  229. "saved_count": "x",
  230. "unread_count": "x",
  231. "dropout_rate": "x",
  232. "average_completion_time": null,
  233. "is_uptodate": "x"
  234. }
  235. },
  236. "User": {
  237. "username": "somedude@example.com"
  238. }
  239. }
  240. ]
  241. }
  242.  
  243. Form/id,Form/version_id,Form/name,Form/category,Form/subcategory,Form/is_template,Form/moderation_status,Form/display_status,Form/use_ssl,Form/modified,Form/Aggregate_metadata/id,Form/Aggregate_metadata/response_count,Form/Aggregate_metadata/submitted_count,Form/Aggregate_metadata/saved_count,Form/Aggregate_metadata/unread_count,Form/Aggregate_metadata/dropout_rate,Form/Aggregate_metadata/average_completion_time,Form/Aggregate_metadata/is_uptodate,User/username
  244. x,x,x,"","","",x,x,x,x,x,x,x,x,x,x,"",x,somedude@example.com
  245. x,x,x,"","","",x,x,x,x,x,x,x,x,x,x,"",x,somedude@example.com
  246.  
  247. require 'csv'
  248. require 'json'
  249.  
  250. @headers = []
  251. file = File.open('file.json')
  252. JSON.parse(file.read).each do |h|
  253. h.keys.each do |key|
  254. @headers << key
  255. end
  256. end
  257. uniq_headers = @headers.uniq
  258. file = File.open('file.json')
  259. finalrow = []
  260.  
  261. JSON.parse(file.read).each do |h|
  262. final = {}
  263. @headers.each do |key2|
  264. final[key2] = h[key2]
  265. end
  266. finalrow << final
  267. end
  268. CSV.open('output.csv' , 'w') do |csv|
  269. csv << uniq_headers
  270. finalrow.each do |deal|
  271. csv << deal.values
  272. end
  273. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement