# Output JSON changing Header column order from Query Table

**URL:** <https://forum.inductiveautomation.com/t/output-json-changing-header-column-order-from-query-table/62716>\
**Category:** Ignition\
**Tags:** ignition81, scripting, perspective\
**Created:** [August 5, 2022, 2:15pm UTC](https://forum.inductiveautomation.com/t/output-json-changing-header-column-order-from-query-table/62716 "2022-08-05T14:15:53Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Neil\_Lewallen](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/neil_lewallen/32/87172_2.png) [@Neil\_Lewallen](https://forum.inductiveautomation.com/u/Neil_Lewallen)\
**Post date:** [August 5, 2022, 2:15pm UTC](https://forum.inductiveautomation.com/t/output-json-changing-header-column-order-from-query-table/62716/1 "2022-08-05T14:15:53Z")

</div>

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.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/a/2/a2f808c34a00c27cdc9305110ebeeb022e1821a5.png)  
Original data return in the format desired, prior to transform.  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/4/d/4d05f74b1c2fbd24608326e03c75b5e9f806d36a.png)  
Post transform.

```auto
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

```

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [August 5, 2022, 2:21pm UTC](https://forum.inductiveautomation.com/t/output-json-changing-header-column-order-from-query-table/62716/2 "2022-08-05T14:21:17Z")

</div>

Your row object is a dictionary. Python dictionaries do not maintain the order of items added. Try using an [OrderedDict](https://docs.python.org/2.7/library/collections.html#collections.OrderedDict) from python’s `collections` module.

Or supply an explicit columns configuration for the table.

---

<div class="post-metadata">

**Author:** ![Neil\_Lewallen](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/neil_lewallen/32/87172_2.png) [@Neil\_Lewallen](https://forum.inductiveautomation.com/u/Neil_Lewallen)\
**Post date:** [August 5, 2022, 2:27pm UTC](https://forum.inductiveautomation.com/t/output-json-changing-header-column-order-from-query-table/62716/3 "2022-08-05T14:27:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Neil\_Lewallen](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/neil_lewallen/32/87172_2.png) [@Neil\_Lewallen](https://forum.inductiveautomation.com/u/Neil_Lewallen)\
**Post date:** [August 5, 2022, 3:54pm UTC](https://forum.inductiveautomation.com/t/output-json-changing-header-column-order-from-query-table/62716/4 "2022-08-05T15:54:18Z")

</div>

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.

```auto
# 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`

```

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [August 5, 2022, 4:03pm UTC](https://forum.inductiveautomation.com/t/output-json-changing-header-column-order-from-query-table/62716/5 "2022-08-05T16:03:35Z")

</div>

`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. }

---

<div class="post-metadata">

**Author:** ![Neil\_Lewallen](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/neil_lewallen/32/87172_2.png) [@Neil\_Lewallen](https://forum.inductiveautomation.com/u/Neil_Lewallen)\
**Post date:** [August 5, 2022, 7:50pm UTC](https://forum.inductiveautomation.com/t/output-json-changing-header-column-order-from-query-table/62716/6 "2022-08-05T19:50:36Z")

</div>

Format stayed the same after changing _row\_value = {}_ to _row\_value\_= OrderedDict()_

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/e/a/ea4908ca6b807d610e4f9faf0f4ef4e79e420d83.png)

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [August 5, 2022, 8:09pm UTC](https://forum.inductiveautomation.com/t/output-json-changing-header-column-order-from-query-table/62716/7 "2022-08-05T20:09:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Neil\_Lewallen](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/neil_lewallen/32/87172_2.png) [@Neil\_Lewallen](https://forum.inductiveautomation.com/u/Neil_Lewallen)\
**Post date:** [August 5, 2022, 8:37pm UTC](https://forum.inductiveautomation.com/t/output-json-changing-header-column-order-from-query-table/62716/8 "2022-08-05T20:37:49Z")

</div>

That was the ticket. Appreciate the help from everyone!
