Python顶级JSON索引

时间:2016-09-30 18:13:48

标签: python json

我从我正在调用的API中获取以下JSON文件:

{
  "14500": [
    {
      "5": {
        "versionName": "VersionOne",
        "expand": "executionSummaries",
        "name": "Regression",
        "projectId": 15006,
        "startDate": "",
        "executionSummaries": {
          "executionSummary": []
        }
      },
      "7": {
        "versionName": "VersionOne",
        "expand": "executionSummaries",
        "versionId": 14500,
        "projectId": 15006,
        "startDate": "19/Sep/16",
        "executionSummaries": {
          "executionSummary": []
        }
      },
      "-1": {
        "versionName": "VersionOne",
        "expand": "executionSummaries",
        "name": "Ad hoc",
        "modifiedBy": "",
        "projectId": 15006,
        "startDate": "",
        "executionSummaries": {
          "executionSummary": []
        }
      },
      "recordsCount": 3
    }
  ],
  "14501": [
    {
      "-1": {
        "versionName": "Version 2",
        "expand": "executionSummaries",
        "projectId": 15006,
        "startDate": "",
        "executionSummaries": {
          "executionSummary": []
        }
      },
      "recordsCount": 1
    }
  ],
}

我需要遍历顶层,下一层(例如“14500”,“5”,“7”等)来查找键和值。因此,例如,我需要搜索整个JSON文件以找到与“回归”匹配的名称,并找到“ProjectID”的数据集值以及可能的其他字符串。我以前通过使用数据[“level1”] [0] [“level2”]等来完成此操作,但在这种情况下,数字将永远不会相同,所以我不知道如何调用它们。我在这里查看了一些帖子后写了以下内容,但它只适用于一个级别,而不是JSON中的下一个级别。

request = requests.get(getCyclesURL, headers=headers)
requestData = json.loads(request.text)
requestDataKeys = requestData.keys

for k in requestDataKeys():
    dictionaryIDs = requestData[k]
    for m in requestDataKeys:
        newDictionaryIDs = requestData[k][m]
        for dict in newDictionaryIDs:
            if dict['versionName'] == versionName:
                versionID = dict['versionID']
                print '%s: %s'%(versionName, versionID)

3 个答案:

答案 0 :(得分:1)

查看令人惊叹的boltons库!它具有remap功能,在您的情况下可能是一种矫枉过正,但是要了解它是一件好事。这是从嵌套数据结构中使用'name': 'Regression'提取所有dicts的优雅方法:

from boltons.iterutils import remap

# Copy your actual data here
data = {'14500': [{'5': {'name': 'Regression'}, '7': {'name': 'Ad hoc'}}]}

regressions = []

def visit(path, key, value):
    if isinstance(value, dict) and value.get('name') == 'Regression':
        # You can do whatever you want here!
        # If you're here then `value` is a dict
        # and its `name` field equals to 'Regression'.
        regressions.append(value)
    return key, value

remap(data, visit=visit, reraise_visit=False)

assert regressions == [{'name': 'Regression'}]

如果您只需要某个级别的词组,您还可以检查path函数中visit的长度。

答案 1 :(得分:0)

def find_in_dicts(d, key, value): #This finds all dictionaries that has the given key and value
    for k, v in d.items():
        if k == key:
            if v == value:
                yield d
        else:
            if isinstance(v, dict):
               for i in find_in_dicts(v, key, value):
                   yield i
            elif isinstance(v, list) and isinstance(v[0], dict):
                for item in v:
                    for i in find_in_dicts(item, key, value):
                        yield i

无论数据结构有多深,这都应该递归工作。我目前无法测试它,但我希望它能在弹性体上给你一些想法。

答案 2 :(得分:0)

这是根据您的确切输入量身定制的部分脚本。如果它在适当的级别找到name: regression,它会打印一些相关的值。

for k, list_of_dicts in requestData.items():
    for d in list_of_dicts:
        for k, v in d.items():
            if isinstance(v, dict) and v.get('name') == "Regression":
                print '%s: %s %s'%(v.get('projectId'),
                                   v.get('versionName'),
                                   v.get('versionId'))