# JSON objects not maintaining order of original dataset

**URL:** <https://forum.inductiveautomation.com/t/json-objects-not-maintaining-order-of-original-dataset/82739>\
**Category:** Ignition\
**Tags:** ignition81, perspective, scripting\
**Created:** [December 5, 2023, 7:50pm UTC](https://forum.inductiveautomation.com/t/json-objects-not-maintaining-order-of-original-dataset/82739 "2023-12-05T19:50:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hw79](https://avatars.discourse-cdn.com/v4/letter/h/838e76/32.png) [@hw79](https://forum.inductiveautomation.com/u/hw79)\
**Post date:** [December 5, 2023, 7:50pm UTC](https://forum.inductiveautomation.com/t/json-objects-not-maintaining-order-of-original-dataset/82739/1 "2023-12-05T19:50:31Z")

</div>

I have a dataset that I'm trying to convert into JSON for a table component. Everything seems to be working fine except the order of the columns is not staying the same as the order of the dataset, which I'd like to preserve. I'm also getting key errors, which I think has something to do with the Audit\_ID being an int instead of a str.

As you can see below, I've had to str() the colValue to get the style to work.:

```auto
def transform(self, value, quality, timestamp):
	outputJSON = []
	
	styleOrange = {"backgroundColor": "#F7901D"}
	styleGreen = {"backgroundColor": "#00AA00"}
	
	for row in range(value.getRowCount()):
		rowObject = {"value": {}, "style": {}}
	
		for col in range(value.getColumnCount()):
			colName = value.getColumnName(col)
			colValue = value.getValueAt(row, col)
	
			rowObject['value'][colName] = colValue
	
			if colName == 'Audit_ID':
				if str(colValue) == '1536':
					rowObject['style'] = styleOrange
				elif str(colValue) == '1539':
					rowObject['style'] = styleGreen
	
		outputJSON.append(rowObject)
	
	return outputJSON

```

My efforts with OrderedDict have proved fruitless so far. Any help or insight is appreciated.

---

<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:** [December 5, 2023, 7:54pm UTC](https://forum.inductiveautomation.com/t/json-objects-not-maintaining-order-of-original-dataset/82739/2 "2023-12-05T19:54:40Z")

</div>

You cannot. Java mapping objects (into which all forms of jython dictionary are coerced in Perspective) do not maintain ordered keys. You **must** use the table's `.columns` property to enforce ordering.

---

<div class="post-metadata">

**Author:** ![hw79](https://avatars.discourse-cdn.com/v4/letter/h/838e76/32.png) [@hw79](https://forum.inductiveautomation.com/u/hw79)\
**Post date:** [December 5, 2023, 7:58pm UTC](https://forum.inductiveautomation.com/t/json-objects-not-maintaining-order-of-original-dataset/82739/3 "2023-12-05T19:58:28Z")

</div>

My dataset is dynamic and columns may get added or dropped depending on the filters applied. I was hoping to add formatting to the columns without explicitly naming them in the .columns field prop

---

<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:** [December 5, 2023, 8:00pm UTC](https://forum.inductiveautomation.com/t/json-objects-not-maintaining-order-of-original-dataset/82739/4 "2023-12-05T20:00:06Z")

</div>

Dynamically generate your columns property from your dataset, too.

> [@hw79](#):
>
> I was hoping

Sorry.

---

<div class="post-metadata">

**Author:** ![hw79](https://avatars.discourse-cdn.com/v4/letter/h/838e76/32.png) [@hw79](https://forum.inductiveautomation.com/u/hw79)\
**Post date:** [December 5, 2023, 8:59pm UTC](https://forum.inductiveautomation.com/t/json-objects-not-maintaining-order-of-original-dataset/82739/5 "2023-12-05T20:59:49Z")

</div>

> [@pturmel](#):
>
> Sorry.

dang...

> [@pturmel](#):
>
> Dynamically generate your columns property from your dataset, too.

So I did that.

```auto
def transform(self, value, quality, timestamp):
    outputObject = []
    
    if value.getRowCount() > 0:
        colObjects = []
        
        for col in range(value.getColumnCount()):
            colName = value.getColumnName(col)
            
            colObject = {
                "field": colName,
                "visible": True,
                "editable": True,
                "render": "auto",
                "justify": "auto",
                "align": "center",
                "resizable": True,
                "sortable": True,
                "filter": {
                    "enabled": False,
                    "visible": "always",
                    "string": {
                        "condition": "contains",
                        "value": ""
                    },
                    "number": {
                        "condition": "greater than",
                        "value": ""
                    },
                    "boolean": {
                        "condition": "true"
                    },
                    "date": {
                        "condition": "date equals",
                        "value": ""
                    }
                },
                "viewPath": "",
                "viewParams": {},
                "boolean": "checkbox",
                "numberFormat": "none",
                "dateFormat": "none",
                "width": 100,
                "strictWidth": False,
                "style": {},
                "header": {
                    "title": colName,
                    "justify": "auto",
                    "align": "center",
                    "style": {}
                },
                "footer": {
                    "title": "",
                    "justify": "auto",
                    "align": "center",
                    "style": {}
                }
            }
            
            colObjects.append(colObject)
        
        outputObject = colObjects
    
    return outputObject

```

It's sort of working, except those things reset when the dataset refreshes. I'm thinking I need to add some logic to change the value (numberFormat: number, for example) based on the column name.
