Python JSON Sorting nested data
If I understood correctly you need to remove the line:
data_obj = json.dumps(json_data,sort_keys=True)
for example:
def sort_json(json_data):
workflows = json_data["data"]["workflows"]["allWorkflows"]
return sorted(workflows, key=lambda d: d["name"])
jd = {
"data": {
"workflows": {
"allWorkflows": [{
"name": "ICX-SLX Config Basic Support",
"version": 106,
"path": "/Workflows/System/Config/Basic Support/ICX-SLX Config Basic Support"
}, {
"name": "ICX Upgrade Firmware",
"version": 37,
"path": "/Workflows/System/Inventory/Upgrade/ICX Upgrade Firmware"
}, {
"name": "Quarantine_PCAP_Flow",
"version": 357,
"path": "/Workflows/System/Security/Quarantine_PCAP_Flow"
}, {
"name": "ICX-MLX Backup Configuration",
"version": 101,
"path": "/Workflows/System/Inventory/Backup/ICX-MLX Backup Configuration"
}, {
"name": "ICX-SLX-MLX Restart Device",
"version": 15,
"path": "/Workflows/System/Inventory/Restart/ICX-SLX-MLX Restart Device"
}, {
"name": "Revert_Quarantine_End_System",
"version": 169,
"path": "/Workflows/System/Security/Revert_Quarantine_End_System"
}]
}
}
}
result = sort_json(jd)
print(result)
Output
[{'version': 37, 'name': 'ICX Upgrade Firmware', 'path': '/Workflows/System/Inventory/Upgrade/ICX Upgrade Firmware'}, {'version': 101, 'name': 'ICX-MLX Backup Configuration', 'path': '/Workflows/System/Inventory/Backup/ICX-MLX Backup Configuration'}, {'version': 106, 'name': 'ICX-SLX Config Basic Support', 'path': '/Workflows/System/Config/Basic Support/ICX-SLX Config Basic Support'}, {'version': 15, 'name': 'ICX-SLX-MLX Restart Device', 'path': '/Workflows/System/Inventory/Restart/ICX-SLX-MLX Restart Device'}, {'version': 357, 'name': 'Quarantine_PCAP_Flow', 'path': '/Workflows/System/Security/Quarantine_PCAP_Flow'}, {'version': 169, 'name': 'Revert_Quarantine_End_System', 'path': '/Workflows/System/Security/Revert_Quarantine_End_System'}]