I have a json data that, when printed out, does not print in the order i set it up.
#!/usr/bin/env python2.7
myjson = {
"timestamp": "2019-05-22T15:25:15.870Z",
"account_name": "BLah, Inc",
"alert_description": "Check Proc Num",
"alert_itemid": "N/A",
"alert_key": "N/A",
"alert_message": "Check Proc Num",
"alert_zabbix_url": "https://blah.com",
"alertcount": "1",
"alertid": "41009",
"checkcount": "13",
"current_state": "problem",
"groups": ["Linux_servers--2", "blah2" ],
"hostid": "10439",
"hostip": "blah",
"hostname": "blah",
"value": "N/A"
}
myjson['account_name'] = "NewBlah, Inc"
myjson['alert_description'] = "Disk Space"
When I run the above code, i get this:
{'value': 'N/A', 'hostid': '10439', 'alertid': '41009', 'alert_key': 'N/A', 'timestamp': '2019-05-22T15:25:15.870Z', 'alert_zabbix_url': 'https://blah.com', 'current_state': 'problem', 'hostname': 'blah', 'alert_message': 'Check Proc Num', 'alertcount': '1', 'groups': ['Linux_servers--2', 'blah2'], 'hostip': 'blah', 'checkcount': '13', 'alert_description': 'Disk Space', 'account_name': 'NewBlah, Inc', 'alert_itemid': 'N/A'}
The problem is, this data is all out of order. I expected the data to look like this (note the timestamp):
{ "timestamp": "2019-05-22T15:25:15.870Z", "account_name": "BLah, Inc", "alert_description": "Check Proc Num", "alert_itemid": "N/A", "alert_key": "N/A", "alert_message": "Check Proc Num", "alert_zabbix_url": "https://blah.com", "alertcount": "1", "alertid": "41009", "checkcount": "13", "current_state": "problem", "groups": ["Linux_servers--2", "blah2" ], "hostid": "10439", "hostip": "blah", "hostname": "blah", "value": "N/A" }
I wanted the timestamp to come first as I specified it in the original data i set in the myjson variable.
I googled this but did not find a working solution. For portability reasons, Im hoping to resolve this using only the default python libraries/modules.