ruby - Append new lines to a csv from json.parse -
more sysadmin (chef) ruby guy, may 5 minute fix. working on task write ruby script pulls json data multiple files, parses it, , writes desired fields single .csv file. pulling metadata aws accounts , putting in accountant friendly format.
got lot of stackoverflow on how solve problem single file, json.parse help.
my issue trying pull same data multiple json files in array. can loop through each file code below.
require 'csv' require "json" delim_file = csv.open("delimited_test.csv", "w") aws_account_list = %w(example example2) aws_account_list.each |account| json_file = file.read(account.to_s + "_aws.json") parsed_json = json.parse(json_file) delim_file = csv.open("delimited_test.csv", "w") # next line problem if ran code multiple times delim_file << ["ebsoptimized", "privatednsname", "keyname", "availabilityzone", "ownerid"] parsed_json['reservations'].each |inner_json| inner_json['instances'].each |instance_json| delim_file << [[instance_json['ebsoptimized'].to_s, instance_json['privatednsname'], instance_json['keyname'], instance_json['placement']['availabilityzone'], inner_json['ownerid']],[]] end delim_file.close end end
however, whenever it, overwrites every time same single row in .csv file. have tried adding \n string end of array, converting array string hashes , doing \n, add line same row overwrites.
how go writing reads each json file, appending each files metadata new row? looks simple case of writing right loop, can't figure out.
you declared file this:
delim_file = csv.open("delimited_test.csv", "w")
to fix issue, have change "w"
"a"
:
delim_file = csv.open("delimited_test.csv", "a")
see docs io#new
description of available file modes. in short, w
creates empty file @ filename, overwriting anyothers, , writes that. a
creates file if doesn't exist, , appends otherwise. because have @ w
, it'll overwrite each time run script. a
, it'll append what's there.
Comments
Post a Comment