title: Jenkins Groovy - accessing map elements
body: <p>I am trying to create docker-compose yaml file with Groovy:</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>def depTemplate = [
                                  version: "3.9", 
                                  services:[
                                      "${serviceName}":[
                                          hostname: "some-hostname",
                                          environment: [],
                                          ports: [],
                                          volumes: [],
                                          restart: ''
                                  ]
                              ]
              ]</code></pre>
  </div>
  </div>
  </p>
  <p>I am trying to add some values to volumes:
  <strong>depTemplate.services.&quot;${serviceName}&quot;.volumes.add(&quot;some volume&quot;)</strong>
  but it fails with error <strong>Cannot get property 'volumes' on null object</strong></p>
  <p>Otherwise, if i declare template in following way:</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>def depTemplate = [
                                  version: "3.9", 
                                  services:[
                                      application:[
                                          hostname: "some-hostname",
                                          environment: [],
                                          ports: [],
                                          volumes: [],
                                          restart: ''
                                  ]
                              ]
              ]</code></pre>
  </div>
  </div>
  </p>
  <p>and then trying to add new volume:  <strong>depTemplate.services.application.volumes.add(&quot;some volume&quot;)</strong> - it works perfectly.
  Also when i am trying to get value from template by index:
  print depTemplate.services[0] - it returns <strong>null</strong>, but when i am trying to get element by name: print depTemplate.services['application'] - it returns valid value:</p>
  <p>{hostname=some-hostname, environment=&quot;some-environment-variables&quot;, ports=&quot;some-ports, volumes=&quot;some-volumes&quot;  restart=}</p>
  <p>So the question is how can i access map element by using variables and how can i access map elements by id?</p>