如何使用键值合并和排序两个json列表

时间:2015-10-13 20:24:29

标签: python json

我可以从API获取组和设备的JSON列表,但是键值不允许我在不操纵返回列表的情况下进行合并。不幸的是,必须使用单独的http请求检索组信息和设备信息。

获取组信息的代码如下所示:

    #Python Code
    import requests
    import simplejson as json
    import datetime
    import pprintpp 

    print datetime.datetime.now().time()

    url = 'https://www.somecompany.com/api/v2/groups/?fields=id,name'
    s = requests.Session()

    ## Ver2 API Authenticaion ##
    headers = {
        'X-ABC-API-ID': 'nnnn-nnnn-nnnn-nnnn-nnnn',
        'X-ABC-API-KEY': 'nnnnnnnn',
        'X-DE-API-ID': 'nnnnnnnn',
        'X-DE-API-KEY': 'nnnnnnnn'
    }
    r = json.loads(s.get((url), headers=headers).text)

    print "Working...Groups extracted"
    groups = r["data"]
    print "*** Ver2 API Groups Information ***"
    pprintpp.pprint (groups)

组的打印输出如下所示:

    #Groups
    [
        {u'id': u'0001', u'name': u'GroupA'},
        {u'id': u'0002', u'name': u'GroupB'},
    ] 

获取设备信息的代码如下所示:

    url = 'https://www.somecompany.com/api/v2/devicess/?limit=500&fields=description,group,id,name'
    r = json.loads(s.get((url), headers=headers).text)

    print "Working...Devices extracted"
    devices = r["data"]
    print "*** Ver2 API Devices Information ***"
    pprintpp.pprint (devices)

设备输出如下所示:

    #Devices
    [
        {
            u'description': u'GroupB 100 (City State)',
            u'group': u'https://www.somecompany.com/api/v2/groups/0002/',
            u'id': u'90001',
            u'name': u'ABC550-3e9',
        },
        {
            u'description': u'GroupA 101 (City State)',
            u'group': u'https://www.somecompany.com/api/v2/groups/0001/',
            u'id': u'90002',
            u'name': u'ABC500-3e8',
        }
    ]

我想要做的是能够将两个JSON列表合并并排序为如下所示的输出:

    #Desired Output
    #Seperated List of GroupA & GroupB Devices
    [
        {u'id': u'0001', u'name': u'GroupA'},

           {
              u'description': u'GroupA 101 (City State)',
              u'group': u'https://www.somecompany.com/api/v2/groups/0001/',
              u'id': u'90002',
              u'name': u'ABC500-3e8',
           },

        {u'id': u'0002', u'name': u'GroupB'},

           {
              u'description': u'GroupB 100 (City State)',
              u'group': u'https://www.somecompany.com/api/v2/groups/0002/',
              u'id': u'90001',
              u'name': u'ABC550-3e9',
           }
    ]

我遇到的一些问题是组和设备输出的关键名称不是唯一的。组中名为“id”的键实际上与设备中名为“group”的键的最后4位数字相同,并且是我希望用于排序的值。此外,组中的“id”和“name”与设备中的“id”和“name”不同。我极其有限的Python技能使这成为一项挑战。任何帮助指出我正确的解决方向将非常感激。

1 个答案:

答案 0 :(得分:0)

该程序产生您想要的输出:

import pprintpp
groups = [
        {u'id': u'0001', u'name': u'GroupA'},
        {u'id': u'0002', u'name': u'GroupB'},
    ]

devices = [
        {
            u'description': u'GroupB 100 (City State)',
            u'group': u'https://www.somecompany.com/api/v2/groups/0002/',
            u'id': u'90001',
            u'name': u'ABC550-3e9',
        },
        {
            u'description': u'GroupA 101 (City State)',
            u'group': u'https://www.somecompany.com/api/v2/groups/0001/',
            u'id': u'90002',
            u'name': u'ABC500-3e8',
        }
    ]

desired = sorted(
    groups + devices,
    key = lambda x: x.get('group', x.get('id')+'/')[-5:-1])

pprintpp.pprint(desired)

或者,如果lambda似乎没有自我记录:

def key(x):
    '''Sort on either the last few digits of x['group'], if that exists,
       or the entirety of x['id'], if x['group'] does not exist.
    '''
    if 'group' in x:
        return x['group'][-5:-1]
    return x['id']
desired = sorted(groups + devices, key=key)