title: Loop over json objects in Ansible
body: <p>I have been stuck to get a particular json object if the value of a key matches a variable (string).</p>
  
  <p>My json file looks like this:</p>
  
  <pre><code>    "totalRecordsWithoutPaging": 1234,
      "jobs": [
          {
              "jobSummary": {
  
                  "totalNumOfFiles": 0,
                  "jobId": 8035,
                  "destClientName": "BOSDEKARLSSP010",
                  "destinationClient": {
                      "clientId": 10,
                      "clientName": "BOSDEKARLSSP010"
                  }
              }
          },
          {
              "jobSummary": {
                  "totalNumOfFiles": 0,
                  "jobId": 9629,
                  "destClientName": "BOSDEKARLSSP006",
                  "destinationClient": {
                      "clientId": 11,
                      "clientName": "BOSDEKARLSSP006"
                  }
              }
          },
                  .....
      ]
  }
  </code></pre>
  
  <p>I read this json with <code>result: "{{ lookup('file','CVExport-short.json') | from_json }}"</code> and I can get only one value of <code>destClientName</code> key with the following code:</p>
  
  <pre><code>- name: Iterate JSON
    set_fact:
      app_item: "{{ item.jobSummary }}"
    with_items: "{{ result.jobs }}"
    register: app_result
  
  - debug:
      var: app_result.results[0].ansible_facts.app_item.destClientName
  </code></pre>
  
  <p>My goal is to get the value of <code>jobId</code>if the value of <code>destClientName</code> matches some other variable or string in any <code>jobSummary</code>.
  I don't still have much knowledge in Ansible. So, any help would be much appreciated.</p>
  
  <p><strong>Update</strong></p>
  
  <p>Ok, I have found one solution. </p>
  
  <pre><code>- name: get job ID
    set_fact: 
      job_id: "{{ item.jobSummary.jobId }}"
    with_items: "{{ result.jobs}}"
    when: item.jobSummary.destClientName == '{{ target_vm }}'
  
  - debug: 
      msg: "{{job_id}}"
  </code></pre>
  
  <p>But I think there might be a better solution than this. Any idea how?</p>