Using toDataSet on an object property that effectively defines a table doesn’t work…
TBC when I have more time…
Why do you expect it would? The docs specify that you either need to supply something which is already a dataset, or two list arguments, where one argument is a list of strings representing the headers (column names) and the second is a list of lists which represent the rows of the dataset.
headers = ['one', 'two']
rows = [['r1c1', 'r1c2'], ['r2c1', 'r2c2']]
my_dataset = system.dataset.toDataSet(headers, rows)
I would not expect the following to work, and would expect a script error in the logs:
my_table_data = [{'one': 'r1c1', 'two': 'r1c2'}, {'one': 'r2c1', 'two': 'r2c2'}]
system.dataset.toDataSet(my_table_data)
Hmm, maybe it should be a feature request instead, i’ll change it.
A dictionary by definition can only have one value per unique key? I think it reasonable that this isn’t implemented, I think that rather your object isn’t defined properly.
For instance shouldn’t this work?
data = {‘headers’:[‘c1’,’c2’,’c3’],’data’:[[‘r1v1’,’r1v2’,’r1v3’],[‘r2v1’,’r2v2’,’r2v3’]]}
ds = system.dataset.toDataSet(data[‘headers’],data[‘data’])
The only reason i’m using an array of dictionaries in this case is due to a bug in passing datasets (I would only ever pass small ones) to popups. Although I do prefer json objects, however I need to use the dataset lookup
function in an expression. I could use a script transform, but I always prefer the non-scripting method if I can help it for the sake of simplicity and performance.
Also, my object is more like Cody’s:
my_table_data = [{'one': 'r1c1', 'two': 'r1c2'}, {'one': 'r2c1', 'two': 'r2c2'}]
def dictToDataset(dict):
data = []
columns = dict.keys()
for key,value in dict.iteritems():
row = []
for column in columns:
row.append([column])
data.append(row)
return system.dataset.toDataSet(columns,data)
Is what I use for simple dictionaries to datasets.
Perhaps I am missing something here, but wont this code give you a dataset where all of the rows have the same value (e.g. the column headers)?
tDict = {'one':'r1c1','two':'r1c2'}
data = []
columns = tDict.keys()
for key,value in tDict.iteritems():
row = []
for column in columns:
row.append([column])
data.append(row)
print data
I would expect the output to be [['r1c1','r2c2']]
but what it actually outputs is [[['one'],['two']],[['one'],['two']]]
I guess I am not understanding what you are using for your dictionary?
Something like this should do the trick then:
def arrDictToDataset(inputData):
data = []
headers = []
for row in tData:
if not headers:
headers = row.keys()
values = []
for k,v in row.iteritems():
values.append(v)
data.append(values)
return system.dataset.toDataSet(headers,data)
Though, admittedly, even this isn’t a full proof function, as dictionaries are unordered, so use with caution. I’m still working on a way to insure the values end up in the correct columns, perhaps a DatasetBuilder is better.