Python Top Level JSON Index
<p>I have the following JSON file from an API I am calling:</p>

<pre><code>{
  "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
    }
  ],
}
</code></pre>

<p>I need to iterate through the top level, and the next level (ex. "14500", and "5", "7", etc..) to find keys and values. So for instance I need to search through the entire JSON file to find the name that matches "Regression" and locate that set of datas values for "ProjectID" and possibly other strings. I had previously done this by using data["level1"][0]["level2"] etc., but in this case the numbers will never be the same so I don't know how to call them. I wrote the following after looking through some posts on here, but it only works for one level, not the next level down in JSON.</p>

<pre><code>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)
</code></pre>

<p>Check out amazing <a href="http://boltons.readthedocs.io/en/latest/" rel="nofollow"><code>boltons</code></a> library! It has a <a href="http://sedimental.org/remap.html" rel="nofollow"><code>remap</code></a> function that may be an overkill in your case, but is a good thing to know about. Here's an elegant way to extract all dicts with <code>'name': 'Regression'</code> from your nested data structure:</p>

<pre><code>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'}]
</code></pre>

<p>If you only need dicts on a certain level, you can also check length of <code>path</code> in the <code>visit</code> function.</p>

python json