Searching JSON, Returning String within the JSON Structure
<p>I have a set of JSON data that looks simular to this:</p>

<pre><code>{"executions": [
{
  "id": 17,
  "orderId": 16,
  "executionStatus": "1",
  "cycleId": 5,
  "projectId": 15006,
  "issueId": 133038,
  "issueKey": "QTCMP-8",
  "label": "",
  "component": "",
  "projectKey": "QTCMP",
  "executionDefectCount": 0,
  "stepDefectCount": 0,
  "totalDefectCount": 0
},
{
  "id": 14,
  "orderId": 14,
  "executionStatus": "1",
  "cycleId": 5,
  "projectId": 15006,
  "issueId": 133042,
  "issueKey": "QTCMP-10",
  "label": "",
  "component": "",
  "projectKey": "QTCMP",
  "executionDefectCount": 0,
  "stepDefectCount": 0,
  "totalDefectCount": 0
    }
  ],
  "currentlySelectedExecutionId": "",
  "recordsCount": 4
}
</code></pre>

<p>I have taken this and parsed it with Python as below:</p>

<pre><code>import json
import pprint

with open('file.json') as dataFile:
    data = json.load(dataFile)
</code></pre>

<p>With this I am able to find sets of data like executions by doing data["executions"] etc.. What I need to be able to do is search for the string "QTCMP-8" within the structure, and then when I find that specific string, find the "id" that is associated with that string. So in the case of QTCMP-8 it would be id 17; for QTCMP-10 it would be 14.</p>

<p>Is this possible? Do I need to convert the data first? Any help is greatly appreciated!</p>

<p>You can't do this in computational order of O(1), at least with what it is like now. The following is a solution with O(n) complexity for each search.</p>

<pre><code>id = None
for dic in executions:
    if dic['issueKey'] == query:
        id = dic['id']
        break
</code></pre>

<p>Doing this in O(1), need a pre-processing of O(n), in which you categorize executions by their <strong>issueKey</strong>, and save inside it whatever information you want.</p>

<pre><code># Preprocessing of O(n)
mapping = dict()
for dic in executions:
    mapping[dic['issueKey']] = {
        'id':  dic['id'],
        'whatever': 'whateverel'
    }

# Now you can query in O(1)

return dic[query]['id']
</code></pre>

<p>You might also want to consider working with <a href="http://mongodb.com/" rel="nofollow">MongoDB</a> or likes of it, if you're doing heavy json querying.</p>

python json