如何在JSON中包含另一个值数据?

时间:2019-07-11 14:26:24

标签: python json

我有以下列表:

label_list_SR1 =  ['17011', '17022']
label_list_SR2 =  ['18011', '18022', '18033']

I。 JSON内容

我使用 RESTCONF 从路由器返回的以下JSON内容:

{
    "Cisco-IOS-XE-native:name": [
        {
            "pname": "SR1",
            "mode": "enable",
            "index": [
                {
                    "idx": 1,
                    "next-label": 17011
                },
                {
                    "idx": 2,
                    "next-label": 17022
                }
            ]
        },
        {
            "pname": "SR2",
            "mode": "enable",
            "index": [
                {
                    "idx": 1,
                    "next-label": 18022
                },
                {
                    "idx": 2,
                    "next-label": 18033
                },
                {
                    "idx": 3,
                    "next-label": 18044
                }
            ]
        }
    ]
}

II。构建JSON文件

函数make_json(label_list, SR_path)构建JSON文件data.json以便将其发送到下一个函数。

def make_json(label_list, SR_path):
    key_1 = 'Cisco-IOS-XE-native:explicit-path'
    key_2 = "name"
    key_2_1 = "pname"
    key_2_2 = "mode"
    key_2_3 = "index"
    data = {
        key_1: {
            key_2: [
                {
                    key_2_1: SR_path,
                    key_2_2: "enable",
                    key_2_3: [
                        {
                            "idx": 1,
                            "next-label": 22
                        }
                    ]
                }
            ]
        }
    }

data_1 = {
        key_1: {
            key_2: [
                {
                    key_2_1: SR_path,
                    key_2_2: "enable",
                    key_2_3: [

                    ]
                }
            ]
        }
    }
for i in range(len(label_list)):
    str_add = {'idx': i + 1, 'next-label': label_list[i]}
    data_1[key_1][key_2][0]['index'].append(str_add)

with open('data.json', 'w') as outfile:
    return json.dump(data_1, outfile, indent=4)

III。 HTTPS PUT

因为我想使用 HTTPS请求并使用 PUT ,所以我实现了功能make_json(label_list, SR_path),以便推送label_list_SR1和{{ 1}}插入我的路由器。

label_list_SR2

当我调用函数def put_explicit_path(self): filepath = 'data.json' with open(filepath) as fh: mydata = fh.read() urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) https_request_index = 'https://' + self.host + '/restconf/data/Cisco-IOS-XE-native:native/ip/explicit-path' headers = {'Content-type': 'application/yang-data+json', 'Accept': 'application/yang-data+json'} r = requests.put(https_request_index, data=mydata, auth=(self.user, self.password), headers=headers, verify=False) if r: print ("New labels have been pushed") 时,然后make_json(label_list_SR1, label_list_SR1)会如下创建:

make_json(label_list_SR1, label_list_SR2)

从字面上看,它仅考虑最后一个函数。我的目标是拥有与我在部分中的问题开头所显示的JSON文件相同的JSON文件。我,但是我不知道该怎么做。我相信函数 "Cisco-IOS-XE-native:name": [ { "pname": "SR2", "mode": "enable", "index": [ { "idx": 1, "next-label": 17011 }, { "idx": 2, "next-label": 17012 }, { "idx": 3, "next-label": 17013 } ] } ] } 中的某个地方。

0 个答案:

没有答案