0

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.

Dev Ops
  • 127
  • 1
  • 10

1 Answers1

2

Notably, this isn't really JSON, it's a Python dictionary literal. And dictionaries by nature are an unordered data type.

An alternative is to use collections.OrderedDict from the standard library: a dictionary that does keep its keys in order. But this won't look anything like JSON, since it has to be initialized using a normal constructor:

my_dict = OrderedDict(timestamp='...', account_name='...', ...)

Now you can treat my_dict like a normal dictionary in most respects, but iterating over it will keep the keys in your original order.

Note also that JSON, according to its specification, doesn't care about the order of keys in an object. So if you eventually output this as JSON and read it back in, the order will be gone.

P.S. In CPython 3.6 and later, normal dictionaries do have an order associated with them. But I'm guessing if you're not already using the latest CPython, changing your interpreter for this isn't really an option.

EDIT: I just learned that dictionaries will preserve order in all implementations starting in 3.7. But the point above stands. If you're not using an implementation/version with this feature already, switching to a whole new implementation of Python is a much larger change than importing collections.OrderedDict.

Draconis
  • 3,209
  • 1
  • 19
  • 31
  • i feel helpless here because there doesnt seem to be a direct answer to this problem. looks like im going to have to try looping over the data and forcing the timestamp field to come first. – Dev Ops May 23 '19 at 02:13
  • @DevOps The problem is, it sounds like you want JSON to do something it was specifically designed not to do. – Draconis May 23 '19 at 02:15