Perspective - Table Columns with Sub Keys

Utilizing Perspective, let's say I have data in the following form (an array of dictionaries with sub-dictionaries):

[
{
"value1": "value",
"sub_dict": {
"value2": "value",
"value3": "value"
}
},
{
"value1": "value",
"sub_dict": {
"value2": "value",
"value3": "value"
}
}
]

I want to display this in a table such that I have columns for "value1", "value2", and "value3". I can get "value1" to show up as a column in the table but I'm having troubles getting the keys in the "sub_dict" to show up as columns in the table (see attached picture sample). Is there some special syntax? Is this even possible? Note, I can get "sub_dict" to show up as a column, but I really want the keys within that dictionary, not the entire dictionary.

sample_table_column

No, you can't really do this. You could give sub_dict a custom renderer view that 'fakes' having multiple columns (not super well), but there's no way to get the table to do this without flattening your data at the source.

1 Like

You could convert it to a list suitable for the table data using a script. Try pasting this into the Designer Script Console.

myData = [
	{
		"value1": "valueA1",
		"sub_dict": {
			"value2": "valueA2",
			"value3": "valueA3"
		}
	},
	{
		"value1": "valueB1",
		"sub_dict": {
			"value2": "valueB2",
			"value3": "valueB3"
		}
	}
]

def convertToList(data):
	output = []
	for datum in data:
		value1 = datum['value1']
		value2 = datum['sub_dict']['value2']
		value3 = datum['sub_dict']['value3']
		output.append([value1, value2, value3])
	return output


convertToList(myData)

@Transistor, yes, thank you for the idea. I think I'm going to have to do something very similar to what you proposed.

You're welcome.

Tip: when posting code ...

1 Like

You could unwrap one layer with this expression:

forEach(
	{path.to.source.data},
	asMap(
		asPairs(
			asMap('value1', it()['value1']),
			it()['sub_dict')
		)
	)
)
1 Like

Yes, but who would understand it? Not me!

1 Like