使用python在JSON文件中搜索特定密钥
JSON文件: 我可以提取TECH-XXX使用python在JSON文件中搜索特定密钥,python,json,Python,Json,JSON文件: 我可以提取TECH-XXX #!/usr/bin/python import sys import json sys.stdout = open('output.txt','wt') datapath = sys.argv[1] data = json.load(open(datapath)) for issue in data['issues']: if len(issue['fields']['subtasks']) == 0:
#!/usr/bin/python
import sys
import json
sys.stdout = open('output.txt','wt')
datapath = sys.argv[1]
data = json.load(open(datapath))
for issue in data['issues']:
if len(issue['fields']['subtasks']) == 0:
print(issue['key'])
针对没有子任务的每个问题(TECH-729
TECH-731)我想从中提取TECH
project": {
"avatarUrls": {
"16x16": "https://jira.corp.company.com/secure/projectavatar?size=xsmall&pid=10001&avatarId=10201",
"24x24": "https://jira.corp.company.com/secure/projectavatar?size=small&pid=10001&avatarId=10201",
"32x32": "https://jira.corp.company.com/secure/projectavatar?size=medium&pid=10001&avatarId=10201",
"48x48": "https://jira.corp.company.com/secure/projectavatar?pid=10001&avatarId=10201"
},
"id": "10001",
"key": "TECH",
"name": "Technology",
"self": "https://jira.corp.company.com/rest/api/2/project/10001"
},
和customfield_10107.id
我尝试了打印(问题['customfield_10107']['id'])
并获得了成功
./tasks1.py 1.json
Traceback (most recent call last):
File "./tasks1.py", line 11, in <module>
print(issue['customfield_10107']['id'])
KeyError: 'customfield_10107'
/tasks1.py 1.json
回溯(最近一次呼叫最后一次):
文件“/tasks1.py”,第11行,在
打印(问题['customfield_10107']['id']
KeyError:'customfield_10107'
键
存在于问题下
和自定义字段_10107
存在于问题['fields']
:
for issue in response["issues"]:
# For issues without subtasks
if len(issue['fields']['subtasks']) == 0:
# Print custom field id
if 'customfield_10107' in issue['fields']:
custom_field = issue['fields']['customfield_10107']
print custom_field['id']
# Print key
if 'key' in issue:
print issue['key']