Output JSON changing Header column order from Query Table

I am having a odd sorting issue that occurs when using the transform to highlight a row of my query table. The original data coming in from the query orders as expected, but the transform re-orders the columns. I am unsure what I am missing through the output json script that is re-ordering.


Original data return in the format desired, prior to transform.

Post transform.

def transform(self, value, quality, timestamp):
# This list will be used to create a JSON like structure that will insert rows for our styles
			
			output_json = []
			#schedItem = self.custom.SchedItem
			# Here we can define what styling on our rows will be.
			style_active = {"backgroundColor": "#8AFF8A"}
			 
			# You could change more than just the background color, for example:
			# style_another_example {"backgroundColor": "#00AA00", "font-weight": "bold"}
			 
			 
			for row in range(value.getRowCount()):
			    row_object = {}
			    row_value = {}
			    row_style = {}
			    for col in range(value.getColumnCount()):   
			        row_value[value.getColumnName(col)] = value.getValueAt(row, col)
			        row_object['value'] = row_value
			         
			        # Here we're checking the name of the column that we want to base our styling on.
			        if value.getColumnName(col) == 'SchedItemID':
			         
			            # Here we're checking for individual values within the column, and applying styling
			            if value.getValueAt(row, col) == self.custom.SchedItemID:
			                row_style = style_active
	    
			        row_object['style'] = row_style
			    output_json.append(row_object)
			return output_json

Your row object is a dictionary. Python dictionaries do not maintain the order of items added. Try using an OrderedDict from python’s collections module.

Or supply an explicit columns configuration for the table.

Thank you. I will read through and get back to you shortly.

I am new to Python so forgive me.

I read the collections link you stated, but wasn’t able to figure out how to correctly set the index. When doing a direct sub of the Row Object with OrderedDict I was able to get the same results as Row Object but I assume because I am just naming the standard dictionary OrderedDict instead of calling it.

# This list will be used to create a JSON like structure that will insert rows for our styles
				from collections import OrderedDict	
				output_json = []
				#schedItem = self.custom.SchedItem
				# Here we can define what styling on our rows will be.
				style_active = {"backgroundColor": "#8AFF8A"}
				 
				# You could change more than just the background color, for example:
				# style_another_example {"backgroundColor": "#00AA00", "font-weight": "bold"}
			
				for row in range(value.getRowCount()):
				    OrderedDict = {}
				    row_value = {}
				    row_style = {}
				    for col in range(value.getColumnCount()):   
				        row_value[value.getColumnName(col)] = value.getValueAt(row, col)
				        OrderedDict['value'] = row_value
				         
				        # Here we're checking the name of the column that we want to base our styling on.
				        if value.getColumnName(col) == 'SchedItemID':
				         
				            # Here we're checking for individual values within the column, and applying styling
				            if value.getValueAt(row, col) == self.custom.SchedItemID:
				                row_style = style_active
		    
				        OrderedDict['style'] = row_style
				    output_json.append(OrderedDict)
				return output_json`Preformatted text`

OrderedDict is a class. Where you had row_value = {}, instead use row_value = OrderedDict(). Everything else the same as your first script.

{ My first comment about row_object should have been row_value. }

Format stayed the same after changing row_value = {} to row_value_= OrderedDict()

You can (and should) explicitly order your columns using the columns array in the table’s properties.

1 Like

That was the ticket. Appreciate the help from everyone!