Reputation: 105
I would like to use the Google bigquery gem (https://rubygems.org/gems/bigquery) to create an Array of table names. So far, this is what I have written:
require 'json'
bqRepsonse = bq.tables('myDataSet')
bqRepsonseCleaned = bqRepsonse.to_s.gsub("=>", ":")
data = JSON.parse(bqRepsonseCleaned)
tableListing = []
data["tableID"]["type"].each do |item|
case item["type"]
when 'TABLE'
bqTableList << item["tableId"]
else
end
end
If I print bqResponse, I get this result:
[{"kind"=>"bigquery#table", "id"=>"curious-idea-532:dataset_test_4.TableA", "tableReference"=>{"projectId"=>"curious-idea-532", "datasetId"=>"dataset_test_4", "tableId"=>"TableA"}, "type"=>"TABLE"}, {"kind"=>"bigquery#table", "id"=>"curious-idea-532:dataset_test_4.TableB", "tableReference"=>{"projectId"=>"curious-idea-532", "datasetId"=>"dataset_test_4", "tableId"=>"TableB"}, "type"=>"TABLE"}, {"kind"=>"bigquery#table", "id"=>"curious-idea-532:dataset_test_4.TableC", "tableReference"=>{"projectId"=>"curious-idea-532", "datasetId"=>"dataset_test_4", "tableId"=>"TableC"}, "type"=>"TABLE"}, {"kind"=>"bigquery#table", "id"=>"curious-idea-532:dataset_test_4.TableD", "tableReference"=>{"projectId"=>"curious-idea-532", "datasetId"=>"dataset_test_4", "tableId"=>"TableD"}, "type"=>"TABLE"}]
And running the code throws and error
`[]': no implicit conversion of String into Integer (TypeError)
Not sure where to correct this. My desired outcome is:
tableListing = ["TableA","TableB","TableC","TableD"]
Thanks in advance for your advice.
Upvotes: 1
Views: 106
Reputation: 6404
Try this:
require 'json'
string = '[{"kind": "bigquery#table", "id": "curious-idea-532:dataset_test_4.TableA", "tableReference" : {"projectId":"curious-idea-532", "datasetId":"dataset_test_4", "tableId":"TableA"}, "type":"TABLE"}, {"kind":"bigquery#table", "id":"curious-idea-532:dataset_test_4.TableB", "tableReference":{"projectId":"curious-idea-532", "datasetId":"dataset_test_4", "tableId":"TableB"}, "type":"TABLE"}, {"kind":"bigquery#table", "id":"curious-idea-532:dataset_test_4.TableC", "tableReference":{"projectId":"curious-idea-532", "datasetId":"dataset_test_4", "tableId":"TableC"}, "type":"TABLE"}, {"kind":"bigquery#table", "id":"curious-idea-532:dataset_test_4.TableD", "tableReference":{"projectId":"curious-idea-532", "datasetId":"dataset_test_4", "tableId":"TableD"}, "type":"TABLE"}]'
data = JSON.parse(string)
tableListing = []
# Here we are iterating over the data instead of its child element
data.each do |item|
case item["type"]
when 'TABLE'
tableListing << item["tableReference"]["tableId"]
else
end
end
puts tableListing
Upvotes: 1