title: unable to ideally parse a json file in ansible
body: <p>Below is my sample JSON file.</p>
  <pre><code>[
     {
        &quot;?xml&quot;: {
           &quot;attributes&quot;: {
              &quot;encoding&quot;: &quot;UTF-8&quot;,
              &quot;version&quot;: &quot;1.0&quot;
           }
        }
     },
     {
        &quot;domain&quot;: [
  
           {
              &quot;server&quot;: [
                 {
                    &quot;name&quot;: &quot;myserv1&quot;
                 },
                 {
                    &quot;ssl&quot;: {
                       &quot;name&quot;: &quot;myserv1&quot;
                    }
                 },
                 {
                    &quot;log&quot;: [
                       {
                          &quot;name&quot;: &quot;myserv1&quot;
                       },
                       {
                          &quot;file-name&quot;: &quot;/web/bea_logs/domains/mydom/myserv1/myserv1.log&quot;
                       }
                    ]
                 }
              ]
           },
           {
              &quot;server&quot;: [
                 {
                    &quot;name&quot;: &quot;myserv2&quot;
                 },
                 {
                    &quot;ssl&quot;: {
                       &quot;name&quot;: &quot;myserv2&quot;
                    }
                 },
                 {
                    &quot;log&quot;: [
                       {
                          &quot;name&quot;: &quot;myserv2&quot;
                       },
                       {
                          &quot;file-name&quot;: &quot;/web/bea_logs/domains/mydom/myserv2/myserv2.log&quot;
                       }
                    ]
                 }
              ]
           }
        ]
     }
  ]
  </code></pre>
  <p>My requirement is to read the json file and store the value in a file like below:</p>
  <pre><code>myserv1_log: &quot;/web/bea_logs/domains/mydom/myserv1/myserv1.log&quot;
  myserv2_log: &quot;/web/bea_logs/domains/mydom/myserv2/myserv2.log&quot;
  </code></pre>
  <p>i.e</p>
  <pre><code>&lt;server&gt;_log: &lt;file-name&gt;
  </code></pre>
  <p>Here is my Ansible play that uses JMESPath query to read the json data.</p>
  <pre><code>- name: Server Names and log details
    set_fact:
      serverlog:  &quot;{{ jsondata | json_query(jmesquery) }}&quot;
    vars:
      jmesquery: '[].domain[].server[*].log[*].[name, &quot;file-name&quot;]'
  
  - name: Print all server names with log details
    debug:
      msg: &quot;{{ item }}&quot;
    with_items:
      - &quot;{{ serverlog }}&quot;
  </code></pre>
  <p>As you can see I get several <code>null</code> values in the output</p>
  <p>output:</p>
  <pre><code>TASK [Print all server names with log details] *********************************
  Wednesday 02 March 2022  03:17:45 -0600 (0:00:00.100)       0:00:04.730 *******
  ok: [localhost] =&gt; (item=[]) =&gt; {
      &quot;msg&quot;: []
  }
  ok: [localhost] =&gt; (item=[[['myserv1', None], [None, '/web/bea_logs/domains/mydom/myserv1/myserv1.log']]]) =&gt; {
      &quot;msg&quot;: [
          [
              [
                  &quot;myserv1&quot;,
                  null
              ],
              [
                  null,
                  &quot;/web/bea_logs/domains/mydom/myserv1/myserv1.log&quot;
              ]
          ]
      ]
  }
  </code></pre>
  <p>I tied to get rid of the <code>null</code> using the below but that too does not get me the desired output:</p>
  <pre><code> - name: test
     set_fact:
       list2: &quot;{{list2  + [item]}}&quot;
     when: item != &quot;null&quot;
     with_items:
       - &quot;{{serverlog}}&quot;
  
   - name: Neww Print all server names with log details
     debug:
       msg: &quot;{{ item }}&quot;
     with_items:
       - &quot;{{ list3 }}&quot;
  </code></pre>
  <p>I also tried the solution proposed on StackOverflow but I get error:</p>
  <pre><code>&quot;{{ jsondata | selectattr('domain', 'defined') | map(attribute='domain') | flatten | map(attribute='server') | flatten | selectattr('log', 'defined') | map(attribute='log') | map('combine') }}&quot;
  </code></pre>
  <p>Output error:</p>
  <pre><code>The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'server'\n\nThe error appears to be in....
  </code></pre>
  <p>Any earlier similar post did not get any working solution is why I decided to post here:</p>
  <p><a href="https://stackoverflow.com/questions/71320672/remove-null-elements-from-list-ansible">remove null elements from list ansible</a></p>
  <p>Kindly suggest.</p>