# Script JSON Format

**URL:** <https://forum.inductiveautomation.com/t/script-json-format/90434>\
**Category:** Ignition\
**Created:** [May 15, 2024, 2:00pm UTC](https://forum.inductiveautomation.com/t/script-json-format/90434 "2024-05-15T14:00:27Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chintan\_Patel](https://avatars.discourse-cdn.com/v4/letter/c/db5fbb/32.png) [@Chintan\_Patel](https://forum.inductiveautomation.com/u/Chintan_Patel)\
**Post date:** [May 15, 2024, 2:00pm UTC](https://forum.inductiveautomation.com/t/script-json-format/90434/1 "2024-05-15T14:00:27Z")

</div>

I tried to output JSON data from Script Console. I see on output window order of JSON object is not correct. If I change key name output order change again. I attached pictures for

 ![JSON Formating 2](https://us1.discourse-cdn.com/inductiveautomation/original/3X/3/c/3c2d894b4117f4d02ca5f9fd10ecd53b75b09545.png)  
 ![JSON Formiting](https://us1.discourse-cdn.com/inductiveautomation/original/3X/0/1/01ca5ca4b8f0fe63becf65dddf4821825073a052.png)  
reference

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [May 15, 2024, 2:05pm UTC](https://forum.inductiveautomation.com/t/script-json-format/90434/2 "2024-05-15T14:05:21Z")

</div>

Ignition uses Jython based on Python 2.7. This version of Python does not preserve dictionary order.

---

<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:** [May 15, 2024, 2:23pm UTC](https://forum.inductiveautomation.com/t/script-json-format/90434/3 "2024-05-15T14:23:06Z")

</div>

Try using a jython [OrderedDict](https://docs.python.org/2.7/library/collections.html#ordereddict-objects) instead of a regular jython dictionary.

---

<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:** [May 15, 2024, 2:24pm UTC](https://forum.inductiveautomation.com/t/script-json-format/90434/4 "2024-05-15T14:24:28Z")

</div>

Consider also just not caring. Virtually every system that _receives_ JSON is tolerant to keys in any order, because the spec doesn't say anything about the ordering of keys.

---

<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:** [May 15, 2024, 2:37pm UTC](https://forum.inductiveautomation.com/t/script-json-format/90434/5 "2024-05-15T14:37:01Z")

</div>

I do think IA should route all JSONification through instances of `TreeMap` with a human- and source-control-friendly case-sensitive [alphanumeric comparator](https://files.inductiveautomation.com/sdk/javadoc/ignition81/8.1.38/com/inductiveautomation/ignition/common/util/Comparators.html) for the keys. Perhaps with an performance exception for payloads going straight to the browser.

Especially including [system.util.jsonEncode()](https://www.docs.inductiveautomation.com/docs/8.1/appendix/scripting-functions/system-util/system-util-jsonEncode). So that all json that ends up on disk (including json from document or other tags) is **deterministic**.

The JSON spec may not care about key order, but lots of other dependent functionality **does**.

---

<div class="post-metadata">

**Author:** ![Chintan\_Patel](https://avatars.discourse-cdn.com/v4/letter/c/db5fbb/32.png) [@Chintan\_Patel](https://forum.inductiveautomation.com/u/Chintan_Patel)\
**Post date:** [May 15, 2024, 2:58pm UTC](https://forum.inductiveautomation.com/t/script-json-format/90434/6 "2024-05-15T14:58:30Z")

</div>

I also tried jsonEncode and OrderDict. It did not perform as expected.

 ![2](https://us1.discourse-cdn.com/inductiveautomation/original/3X/8/7/875956d405adde05b1e84081934fe8ed208ef408.png)

---

<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:** [May 15, 2024, 3:09pm UTC](https://forum.inductiveautomation.com/t/script-json-format/90434/7 "2024-05-15T15:09:12Z")

</div>

Create your payload directly with OrderedDict, not in a regular dict.

```python
import json
from collections import OrderedDict

payload=OrderedDict()
payload['Version'] = '1.0.0'
payload['Timestamp'] = 80
payload['CurrMachSpeed'] = OrderedDict(Test=30)

print json.dumps(payload)

```

Yielded this for me:

```python
>>> 
{"Version": "1.0.0", "Timestamp": 80, "CurrMachSpeed": {"Test": 30}}
>>>

```

(Please learn to post code and results **as code** on this forum. Use the "preformatted text" button in the forum comment editor. Use screenshots only for context that cannot be conveyed otherwise.)

---

<div class="post-metadata">

**Author:** ![jlandwerlen](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@jlandwerlen](https://forum.inductiveautomation.com/u/jlandwerlen)\
**Post date:** [May 15, 2024, 3:42pm UTC](https://forum.inductiveautomation.com/t/script-json-format/90434/8 "2024-05-15T15:42:14Z")

</div>

May I ask the importance of being in a specific order? As Paul suggested, why care?

---

<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:** [May 15, 2024, 3:46pm UTC](https://forum.inductiveautomation.com/t/script-json-format/90434/9 "2024-05-15T15:46:24Z")

</div>

For browser and API traffic, it truly doesn't matter. Unordered (HashMaps) are fastest and should be used.

For human review, unordered keys are extremely hard for eyes to handle, unless there are only a few entries.

For source code control, unordered keys produce huge diffs for simple changes, and make merges extremely difficult.

Strictly speaking, they should also be made "pretty" with indentations and line breaks. The latter are especially important for source code control.

---

<div class="post-metadata">

**Author:** ![Chintan\_Patel](https://avatars.discourse-cdn.com/v4/letter/c/db5fbb/32.png) [@Chintan\_Patel](https://forum.inductiveautomation.com/u/Chintan_Patel)\
**Post date:** [May 15, 2024, 3:52pm UTC](https://forum.inductiveautomation.com/t/script-json-format/90434/10 "2024-05-15T15:52:38Z")

</div>

Thank you..
