Skip to content

Instantly share code, notes, and snippets.

@timgentry
Created October 18, 2018 12:41
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timgentry/580288e3b9050b548917e937da78b3ff to your computer and use it in GitHub Desktop.
Save timgentry/580288e3b9050b548917e937da78b3ff to your computer and use it in GitHub Desktop.
Converts PowerBI JSON payload to CSV
require 'json'
require 'csv'
hash = JSON.parse(File.read('querydata.json'))
CSV.open('data.csv', 'w') do |csv|
hash['results'].each do |result|
data = result['result']['data']
descriptor = data['descriptor']
dsr = data['dsr']
version = dsr['Version']
raise "unrecognised version #{version.inspect}" unless version == 2
dsr['DS'].each do |dataset|
# dataset["N"]
value_dicts = dataset['ValueDicts']
dataset['PH'].each do |ph|
structure = []
ph['DM0'].each do |dm|
if dm.key?('S')
structure = dm['S']
# puts structure.inspect
csv << structure.map do |s|
label_key = s['N']
group_key = descriptor['Select'].select { |a| a['Value'] == label_key }
.first['GroupKeys'].first
group_key['Source']['Property']
end
end
next if dm['C'].nil?
# puts dm.inspect
row = dm['C'].map.with_index do |column, i|
dictionary_key = structure[i]['DN']
value_dict = value_dicts[dictionary_key]
# puts [column, i, dictionary_key, value_dict.length].inspect
column.is_a?(String) ? column : value_dict[column]
end
# puts row.inspect
csv << row
# dm['R']
end
end
# dataset["IC"]
end
end
end
@alpha-beta-soup
Copy link

alpha-beta-soup commented Jan 8, 2024

Is there a name for this schema? Searching for things like "Power BI JSON payload" just leads to countless threads about ingesting JSON with Power BI, not consuming this output.

@timgentry
Copy link
Author

@alpha-beta-soup not that I'm aware of, but let me know if you find a schema name.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment