python3大型json解析keyerror

时间:2019-03-10 06:29:52

标签: python json python-3.x

尝试使用Python 3.6解析json,我遇到了困难。我需要获取所有的volumeMounts。在遇到麻烦之后,我已经能够一路解析为“容器”。

def get_dc():
    auth_token = os.environ['OCPTOKEN']
    url = os.environ['OCPURL']+"/apis/apps.openshift.io/v1/deploymentconfigs"
    header = {"Authorization": "bearer " + auth_token}
    response = requests.get(url,headers=header,verify=False)
    if(response.ok):
            dc_list = json.loads(response.content)
            name = []
            vol_list = []
            vol_name = []
            vol_path = []
            items = dc_list['items']
            for item in items:
                    name.append(item['metadata']['name'])
                    vol_list = json.dumps(item['spec']['template']['spec']['containers'])
                    vol_list2 = json.loads(vol_list)
                    for vol in vol_list:
                          vol_name.append(vol['volumeMounts'][0]['name'])
                          vol_path.append(vol['volumeMounts']['mountPath'])

            return json.dumps(vol_name, sort_keys=True, indent=4)
            #return vol_list2.keys()
    else:
            return response.raise_for_status()`

JSON

[
{
    "metadata": {
        "annotations": {
            "openshift.io/generated-by": "OpenShiftNewApp"
        },
        "creationTimestamp": "2019-03-09T03:20:16Z",
        "generation": 4,
        "labels": {
            "app": "sonarqube",
            "deploymentconfig": "postgresql-sonarqube"
        },
        "name": "postgresql-sonarqube",
        "namespace": "openshift-metrics",
        "resourceVersion": "4346904",
        "selfLink": "/apis/apps.openshift.io/v1/namespaces/openshift-metrics/deploymentconfigs/postgresql-sonarqube",
        "uid": "4295a7a8-421a-11e9-acaa-000c29897d8e"
    },
    "spec": {
        "replicas": 1,
        "revisionHistoryLimit": 10,
        "selector": {
            "app": "sonarqube",
            "deploymentconfig": "postgresql-sonarqube"
        },
        "strategy": {
            "activeDeadlineSeconds": 21600,
            "recreateParams": {
                "timeoutSeconds": 600
            },
            "resources": {},
            "type": "Recreate"
        },
        "template": {
            "metadata": {
                "annotations": {
                    "openshift.io/generated-by": "OpenShiftNewApp"
                },
                "creationTimestamp": null,
                "labels": {
                    "app": "sonarqube",
                    "deploymentconfig": "postgresql-sonarqube"
                }
            },
            "spec": {
                "containers": [
                    {
                        "env": [
                            {
                                "name": "POSTGRESQL_USER",
                                "value": "sonar"
                            },
                            {
                                "name": "POSTGRESQL_PASSWORD",
                                "value": "QcShxJbo0WnMTJyM"
                            },
                            {
                                "name": "POSTGRESQL_DATABASE",
                                "value": "sonar"
                            }
                        ],
                        "image": "docker-registry.default.svc:5000/openshift/postgresql@sha256:9de77ff85fe737328db17d72b70c78d09e1fef5a81f0dd1f6fb78f6a14fc743d",
                        "imagePullPolicy": "IfNotPresent",
                        "livenessProbe": {
                            "failureThreshold": 3,
                            "initialDelaySeconds": 30,
                            "periodSeconds": 10,
                            "successThreshold": 1,
                            "tcpSocket": {
                                "port": 5432
                            },
                            "timeoutSeconds": 1
                        },
                        "name": "postgresql",
                        "ports": [
                            {
                                "containerPort": 5432,
                                "protocol": "TCP"
                            }
                        ],
                        "readinessProbe": {
                            "exec": {
                                "command": [
                                    "/bin/sh",
                                    "-i",
                                    "-c",
                                    "psql -h 127.0.0.1 -U $POSTGRESQL_USER -q -d $POSTGRESQL_DATABASE -c 'SELECT 1'"
                                ]
                            },
                            "failureThreshold": 3,
                            "initialDelaySeconds": 5,
                            "periodSeconds": 10,
                            "successThreshold": 1,
                            "timeoutSeconds": 1
                        },
                        "resources": {
                            "limits": {
                                "memory": "256Mi"
                            }
                        },
                        "securityContext": {
                            "capabilities": {},
                            "privileged": false
                        },
                        "terminationMessagePath": "/dev/termination-log",
                        "terminationMessagePolicy": "File",
                        "volumeMounts": [
                            {
                                "mountPath": "/var/lib/pgsql/data",
                                "name": "postgresql-data"
                            }
                        ]
                    }
                ],
                "dnsPolicy": "ClusterFirst",
                "restartPolicy": "Always",
                "schedulerName": "default-scheduler",
                "securityContext": {},
                "terminationGracePeriodSeconds": 30,
                "volumes": [
                    {
                        "name": "postgresql-data",
                        "persistentVolumeClaim": {
                            "claimName": "postgresql-sonarqube-data"
                        }
                    }
                ]
            }
        },
        "test": false,
        "triggers": [
            {
                "imageChangeParams": {
                    "automatic": true,
                    "containerNames": [
                        "postgresql"
                    ],
                    "from": {
                        "kind": "ImageStreamTag",
                        "name": "postgresql:9.5",
                        "namespace": "openshift"
                    },
                    "lastTriggeredImage": "docker-registry.default.svc:5000/openshift/postgresql@sha256:9de77ff85fe737328db17d72b70c78d09e1fef5a81f0dd1f6fb78f6a14fc743d"
                },
                "type": "ImageChange"
            },
            {
                "type": "ConfigChange"
            }
        ]
    },
    "status": {
        "availableReplicas": 1,
        "conditions": [
            {
                "lastTransitionTime": "2019-03-09T03:29:40Z",
                "lastUpdateTime": "2019-03-09T03:29:40Z",
                "message": "Deployment config has minimum availability.",
                "status": "True",
                "type": "Available"
            },
            {
                "lastTransitionTime": "2019-03-09T03:29:41Z",
                "lastUpdateTime": "2019-03-09T03:29:41Z",
                "message": "replication controller \"postgresql-sonarqube-3\" successfully rolled out",
                "reason": "NewReplicationControllerAvailable",
                "status": "True",
                "type": "Progressing"
            }
        ],
        "details": {
            "causes": [
                {
                    "type": "Manual"
                }
            ],
            "message": "manual change"
        },
        "latestVersion": 3,
        "observedGeneration": 4,
        "readyReplicas": 1,
        "replicas": 1,
        "unavailableReplicas": 0,
        "updatedReplicas": 1
    }
}
]

当我尝试调用volumeMounts时,收到以下消息:

 Traceback (most recent call last):
  File "test.py", line 96, in <module>
    output = get_dc()
  File "test.py", line 52, in get_dc
    vol_name.append(vol['volumeMounts']['name'])
  TypeError: string indices must be integers

任何帮助将不胜感激。

更新: 添加了代码,并使用vol_name.append(vol ['volumeMounts'] [0] ['name'])进行了尝试,仍然获得字符串索引必须为整数

2 个答案:

答案 0 :(得分:2)

据我从JSON的以下部分了解,volumeMounts是一个数组,而不是字典

  

“ volumeMounts”:[                               {                                   “ mountPath”:“ / var / lib / pgsql / data”,                                   “ name”:“ postgresql-data”                               }                           ]

如果是这样,应该有类似 改为vol_name.append(vol['volumeMounts'][0]['name'])

答案 1 :(得分:0)

在JSON中,您提供了timeout tick timeout tick timeout tick timeout tick 键,该键包含一个包含词典的列表。请尝试以下操作:

volumeMounts