[英]python3 large json parse keyerror
Trying to parse json using Python 3.6, I am running into difficulties. 尝试使用Python 3.6解析json时,遇到了麻烦。 I need to get all of the volumeMounts. 我需要获取所有的volumeMounts。 I have been able to parse all of the way down to 'containers', after that I run in to trouble. 在遇到麻烦之后,我能够一路解析为“容器”。
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 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
}
}
]
When I try to call the volumeMounts, I receive the following: 当我尝试调用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
Any help would be much appreciated. 任何帮助将非常感激。
UPDATE: Added the code, tried it with vol_name.append(vol['volumeMounts'][0]['name']), still getting the string indices must be integers 更新:添加了代码,并使用vol_name.append(vol ['volumeMounts'] [0] ['name'])进行了尝试,仍然得到的字符串索引必须是整数
As I understand from the following part of JSON, volumeMounts is an array, not a dict 据我从JSON的以下部分了解,volumeMounts是一个数组,而不是字典
"volumeMounts": [ { "mountPath": "/var/lib/pgsql/data", "name": "postgresql-data" } ] “ volumeMounts”:[{“ mountPath”:“ / var / lib / pgsql / data”,“ name”:“ postgresql-data”}]
If it is so, there should be something like vol_name.append(vol['volumeMounts'][0]['name'])
instead 如果是这样,应该使用类似于vol_name.append(vol['volumeMounts'][0]['name'])
之类的东西
在JSON中,您提供了volumeMounts
键包含一个包含字典的列表。请尝试以下操作:
vol_name.append(vol['volumeMounts'][0]['name'])
Found the solution, using .get() for the vol_list. 找到了解决方案,将.get()用于vol_list。 Working function: 工作功能:
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 = item['spec']['template']['spec']['containers'][0]
for vol in vol_list.get('volumeMounts', []):
vol_name.append(vol['name'])
vol_path.append(vol['mountPath'])
return json.dumps(vol_name, sort_keys=True, indent=4)
#return vol_list2.keys()
else:
return response.raise_for_status()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.