Python JSON对嵌套数据进行排序

时间:2018-12-03 18:53:43

标签: python json sorting

试图找到对嵌套json数据进行排序的最简单方法。到目前为止,我尝试过的所有操作似乎都失败了。 我正在使用Python 2.7,并尝试按名称属性进行排序。 这是我尝试过的众多python示例之一:

    def sort_json(self,json_data):
        data_obj = json.dumps(json_data,sort_keys=True)
        return sorted(data_obj["data"]["workflows"]["allWorkflows"], 
        key=lambda d: d["name"])

这是未排序的json的示例:

{
  "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"
      }]
    }
  }
}

任何工作示例都将不胜感激。

2 个答案:

答案 0 :(得分:0)

如果我理解正确,则需要删除以下行:

data_obj = json.dumps(json_data,sort_keys=True) 

例如:

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)

输出

[{'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'}]

答案 1 :(得分:0)

嗨,比尔:我认为并发症来自json文件的三层嵌套。一旦我删除了开销层并制作了{na​​me:version,path}的新字典,然后我就可以进行排序:(这可能比您想要的要麻烦一些,但至少这使我明白了这个问题。希望对您有所帮助!)

import os
import shutil

def shorting_algorithm():

    file_list = []
    directory = input("Give the directory you want to search.")
    newdir = "D:\\Torrents\\Complete\\Udemy"
    name = "'" + input("Give the name of the files you want to move.") + "'"
    xlist = os.listdir(directory)
    print(xlist)
    print(name)


    for files in xlist:
        if name in files:
            shutil.move(directory + files,newdir)


shorting_algorithm()