Skip to content
Snippets Groups Projects
Commit 8fbff572 authored by Otto Jonassen Wittner's avatar Otto Jonassen Wittner
Browse files

Added necessary sorting of json components

parent 24fba475
Branches
No related tags found
No related merge requests found
#!/usr/bin/python3
import json
import hashlib
testid_obj = {}
testid_obj["observer_ip"] = "127.0.0.1" # Assuming locahost as sender of results
testid_obj["test"] = "trace"
testid_obj["tool"] = "traceroute"
from operator import itemgetter
# replace all three {} with OrderedDict() for python <= 3.6
def sorted_json(js, result):
def norm_str(s):
# because of str special handling of single quotes
return str(s).replace("'", '"')
if type(js) in [int, str, bool, float] or js is None:
return js
if type(js) == list:
res = [sorted_json(i, {}) for i in js]
return sorted(res, key=norm_str)
items = sorted(js.items(), key=itemgetter(0))
for k, v in items:
result[k] = sorted_json(v, {})
return result
#testid_obj = {}
#testid_obj["observer_ip"] = "127.0.0.1" # Assuming locahost as sender of results
#testid_obj["test"] = "trace"
#testid_obj["tool"] = "traceroute"
fd = open('testid.json')
unsorted_testid_obj = json.load(fd)
testid_obj = sorted_json(unsorted_testid_obj, {})
j = json.dumps(testid_obj)
j = j.replace(" ","")
j = j.encode('utf-8')
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment