# Ordered Dictionaries

**URL:** <https://forum.inductiveautomation.com/t/ordered-dictionaries/45949>\
**Category:** Ignition\
**Tags:** ignition80, scripting\
**Created:** [April 23, 2021, 8:43am UTC](https://forum.inductiveautomation.com/t/ordered-dictionaries/45949 "2021-04-23T08:43:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![limjx](https://avatars.discourse-cdn.com/v4/letter/l/7c8e57/32.png) [@limjx](https://forum.inductiveautomation.com/u/limjx)\
**Post date:** [April 23, 2021, 8:43am UTC](https://forum.inductiveautomation.com/t/ordered-dictionaries/45949/1 "2021-04-23T08:43:13Z")

</div>

Hi,

I would like create a json file in a specific format, however there are some restriction involved.

1. Declared dictionaries not in my specific key order (d,c,a,b).

```auto
from collections import OrderedDict

mylist = []

myOrderedDict = OrderedDict()
myOrderedDict['d'] = 4
myOrderedDict['c'] = 3
myOrderedDict['a'] = 1
myOrderedDict['b'] = mylist

myOrderedDict #Intended result
dict(myOrderedDict) #dict changed my result

myDict = {}
myDict['d'] = 4
myDict['c'] = 3
myDict['a'] = 1
myDict['b'] = mylist

myDict #dict changed my result

```

1. After assigning my dictionaries, I would like to encode the dictionaries then write to file.  
Below are two different method that I had tried but still unable to get the json string format that I required (d,c,a,b).

```auto
jsonStr = system.util.jsonEncode(dict(myOrderedDict),5)
print jsonStr

import json
jsonNewStr = json.dumps({	'd':4,
						'c':3,
						'a':1,
						'b':mylist},
			sort_keys=False,
			indent=4, separators=(',', ':'))
print jsonNewStr		

```

I have also checked that ignition running on python 2.7.  
[https://docs.inductiveautomation.com/display/DOC81/Python+Scripting](https://docs.inductiveautomation.com/display/DOC81/Python+Scripting)

For python 3.7 onward dictionary will be in order according to source code (Reference from SO)

> <https://stackoverflow.com/questions/1867861/how-to-keep-keys-values-in-same-order-as-declared>

Is there a way to workaround or could I be doing it wrong?  
Appreciate for any advise and thank you in advance.

---

<div class="post-metadata">

**Author:** ![victordcq](https://avatars.discourse-cdn.com/v4/letter/v/f6c823/32.png) [@victordcq](https://forum.inductiveautomation.com/u/victordcq)\
**Post date:** [April 23, 2021, 9:59am UTC](https://forum.inductiveautomation.com/t/ordered-dictionaries/45949/2 "2021-04-23T09:59:35Z")

</div>

I dont think transforming an ordered list to json will not preserve its order.  
If order is that important you will have to add in keys from when they where entered tho i guess.

```auto
myOrderedDict = OrderedDict()
myOrderedDict[1] = {"d":4}
myOrderedDict[2] = {"c":3}
myOrderedDict[3] = {"a":1}
myOrderedDict[4] = {"b":[]}

myOrderedDict
jsonStr = system.util.jsonEncode(myOrderedDict)

```

---

<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:** [April 23, 2021, 4:47pm UTC](https://forum.inductiveautomation.com/t/ordered-dictionaries/45949/3 "2021-04-23T16:47:18Z")

</div>

Python’s builtin JSON library has a `sort_keys` parameter:  
[https://docs.python.org/2.7/library/json.html](https://docs.python.org/2.7/library/json.html)

That said, relying on explicit key ordering in JSON objects/Python dictionaries is almost certainly an antipattern.

---

<div class="post-metadata">

**Author:** ![wking](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/wking/32/21823_2.png) [@wking](https://forum.inductiveautomation.com/u/wking)\
**Post date:** [April 23, 2021, 8:55pm UTC](https://forum.inductiveautomation.com/t/ordered-dictionaries/45949/4 "2021-04-23T20:55:48Z")

</div>

![dictionary](https://us1.discourse-cdn.com/inductiveautomation/original/3X/7/1/71a8e69990814b9c5b5203645238831f651c3757.jpeg)

---

<div class="post-metadata">

**Author:** ![limjx](https://avatars.discourse-cdn.com/v4/letter/l/7c8e57/32.png) [@limjx](https://forum.inductiveautomation.com/u/limjx)\
**Post date:** [April 26, 2021, 5:02am UTC](https://forum.inductiveautomation.com/t/ordered-dictionaries/45949/5 "2021-04-26T05:02:26Z")

</div>

Yes, the sort\_keys parameter is what I intended to use but I got an error previously by adding a cursive bracket to my OrderedDict in the json.dumps.

The code below is working for me now. Thanks!

```auto
from collections import OrderedDict

mylist = []

myOrderedDict = OrderedDict()
myOrderedDict['d'] = 4
myOrderedDict['c'] = 3
myOrderedDict['a'] = 1
myOrderedDict['b'] = mylist

import json
jsonNewStr = json.dumps(myOrderedDict,
			sort_keys=False,
			indent=4, separators=(',', ':'))

system.file.writeFile("D:\NewJson.json", jsonNewStr)

```
