Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python在JSON文件中搜索特定密钥_Python_Json - Fatal编程技术网

使用python在JSON文件中搜索特定密钥

使用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:

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:
                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']