title: How to access key/pair values within objects inside of an array in Ansible?
body: <p>So, we are wanting to propagate a list of logged-in users to our hosts.
  We have a batch script that does this for us and parses it to JSON format. </p>
  
  <pre><code>    - name: Process win_shell output
        set_fact:
          qusers: "{{ qusers_ps.stdout | from_json }}"
  
      - debug:
          var: qusers
          verbosity: 2
  </code></pre>
  
  <p>This debug outputs the following:</p>
  
  <pre><code>{
      "qusers": {
          "Server1": []
      },
      "_ansible_verbose_always": true,
      "_ansible_no_log": false,
      "changed": false
  }
  {
      "qusers": {
          "Server2": [
              {
                  "USERNAME": "user102",
                  "SESSIONNAME": "rdp-tcp#0",
                  "ID": "6",
                  "STATE": "Active",
                  "IDLE TIME": "32",
                  "LOGON TIME": "5/29/2020 9:13 AM"
              }
          ]
      },
      "_ansible_verbose_always": true,
      "_ansible_no_log": false,
      "changed": false
  }
  {
      "qusers": {
          "Server3": [
              {
                  "USERNAME": "user183",
                  "SESSIONNAME": "",
                  "ID": "49",
                  "STATE": "Disc",
                  "IDLE TIME": "14:34",
                  "LOGON TIME": "5/28/2020 7:58 AM"
              },
              {
                  "USERNAME": "user103",
                  "SESSIONNAME": "",
                  "ID": "51",
                  "STATE": "Disc",
                  "IDLE TIME": "18:26",
                  "LOGON TIME": "5/28/2020 8:18 AM"
              },
              {
                  "USERNAME": "user148",
                  "SESSIONNAME": "",
                  "ID": "52",
                  "STATE": "Disc",
                  "IDLE TIME": "17:10",
                  "LOGON TIME": "5/28/2020 9:08 AM"
              }
          ]
      },
      "_ansible_verbose_always": true,
      "_ansible_no_log": false,
      "changed": false
  }
  </code></pre>
  
  <p>So you can see that debug returns JSON with the server name as the root element, and then an array of objects with key/pair values.
  We need to be able to query values within these array objects, such as 'USERNAME' and 'IDLE TIME' for each server. I've been able to do this statically (shown below) to grab the user of the first array object, but I can't figure out how to do this dynamically. </p>
  
  <pre><code>    - name: Test selecting JSON output and register as array
        debug:
          msg: "{{ item.value }}"
        loop: "{{ q('dict', qusers) }}"
        register: user_op
  
      - name: Set array variable
        set_fact:
          user_array: "{{ user_op.results }}"
  
      - name: Print item value # Prints array object as is
        debug:
          msg: "{{ item.item.value }} "
        with_items: "{{ user_array }}"
  
      - name: Test pulling JSON from array with conditional # This one works, but only grabs the first user
        debug: 
          msg: "{{ item.item.value[0]['USERNAME'] }} is logged into {{item.item.key }}"
        with_items: "{{ user_array }}"
        when: item.item.value != [] 
  </code></pre>
  
  <p>The last play is the only time I've ever been able to reach a specific key/pair element. How do I modify this to dynamically perform this operation for every user session object in the array instead of only the first, and without hard coding numbers??</p>