# Had some problems with json strings

**URL:** <https://forum.inductiveautomation.com/t/had-some-problems-with-json-strings/83265>\
**Category:** Ignition\
**Tags:** ignition81, scripting\
**Created:** [December 18, 2023, 4:32pm UTC](https://forum.inductiveautomation.com/t/had-some-problems-with-json-strings/83265 "2023-12-18T16:32:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![wangjun\_ming\_1996](https://avatars.discourse-cdn.com/v4/letter/w/ee59a6/32.png) [@wangjun\_ming\_1996](https://forum.inductiveautomation.com/u/wangjun_ming_1996)\
**Post date:** [December 18, 2023, 4:32pm UTC](https://forum.inductiveautomation.com/t/had-some-problems-with-json-strings/83265/1 "2023-12-18T16:32:13Z")

</div>

I'm using ignition to get the data via api and I get a string of json type.

```auto
import json
import urllib2
url="http://localhost:8080/EProduction/BinWeight"
formData={
	"part":"637724000A",
    "bldg":"ACP-105",
    "startTime":"2023-12-11 06:30:00",
    "stopTime":"2023-12-11 16:29:59"
}
jsonData = json.dumps(formData)
request = urllib2.Request(url, data=jsonData,headers={'Content-Type': 'application/json'})
response = urllib2.urlopen(request)
respData = response.read()
print respData
respData = system.util.jsonDecode(respData)
print "jsonDecode:",type(respData)
print "jsonDecode:",respData

```

* * *

**result** ：

{  
"status":10000,  
"msg":"SUCCESS",  
"data":[  
{  
"lot":"12092 / 21",  
"contQuantity":226.20000000000002  
},  
{  
"lot":"12092 / 45",  
"contQuantity":227.34999999999  
},  
{  
"lot":"12094 / 246",  
"contQuantity":226.5  
},  
{  
"lot":"12094 / 6",  
"contQuantity":224.15  
},  
{  
"lot":"12097 / 245",  
"contQuantity":227.05  
},  
{  
"lot":"12097 / 60",  
"contQuantity":220.1  
}  
]  
}

jsonDecode: \<type 'dict'\>  
jsonDecode: {u'msg': 'SUCCESS', u'data': [{u'contQuantity': 226.20000000000002, u'lot': '12092 / 21'}, {u'contQuantity': 227.34999999999, u'lot': '12092 / 45'}, {u'contQuantity': 226.5, u'lot': '12094 / 246'}, {u'contQuantity': 224.15, u'lot': '12094 / 6'}, {u'contQuantity': 227.05, u'lot': '12097 / 245'}, {u'contQuantity': 220.1, u'lot': '12097 / 60'}], u'status': 10000}

After system.util.jsonDecode() function conversion, dict type key-value pairs inside the change, it seems to be in accordance with the java HashList type sorting  
This is not what I want.  
I'd like the order of the converted structures to still be as returned by the api and that will help me to manage the data in a unified way.  
How do I solve this problem?

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [December 18, 2023, 4:35pm UTC](https://forum.inductiveautomation.com/t/had-some-problems-with-json-strings/83265/2 "2023-12-18T16:35:02Z")

</div>

Why does it matter what order they are in? Objects are accessed by key, and by definition a JSON object is unordered (and so are python dictionaries and Java HashMaps).

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [December 18, 2023, 5:05pm UTC](https://forum.inductiveautomation.com/t/had-some-problems-with-json-strings/83265/3 "2023-12-18T17:05:30Z")

</div>

You'd need to make your own json parser and use `collections.OrderedDict`. But don't do it. I really doubt you actually need to keep the 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:** [December 18, 2023, 5:12pm UTC](https://forum.inductiveautomation.com/t/had-some-problems-with-json-strings/83265/4 "2023-12-18T17:12:10Z")

</div>

> [@Kevin.Herron](#):
>
> by definition a JSON object is unordered

Indeed, but it makes JSON extremely unfriendly to source code control. There are places where [SortedMap\<String, Object\>](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/SortedMap.html) should be used instead, preferably with an [alphanumeric comparator](https://files.inductiveautomation.com/sdk/javadoc/ignition81/8.1.30/com/inductiveautomation/ignition/common/util/Comparators.html#alphaNumeric()) for the keys.

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [December 18, 2023, 6:45pm UTC](https://forum.inductiveautomation.com/t/had-some-problems-with-json-strings/83265/5 "2023-12-18T18:45:43Z")

</div>

For display or troubleshooting, sometimes you _do_ want the order preserved. An OrderedDict can make that easier. Then switch to dict for production.

Last time the OP was here, he was using 7.9, so my bet is that it will go to a dataset by the time it's all done.

YMMV, depending on the datset size, as OrderedDict is slower than dict.

```python
def toOrderedDict(jsonIn):
	''' A helper function to convert a flat array of key-value pairs 
    	in JSON to an OrderedDict.
	'''
	import json
	from collections import OrderedDict
	
	return json.loads(jsonIn, object_pairs_hook=OrderedDict)

```
