title: How to create JSON using map in groovy
body: <p>In groovy how can I create ouput json in the below two structure by using the below input. I have done in java but I'm not getting how can I do in groovy.</p>
  <p>INPUT:</p>
  <pre><code>{
    &quot;name&quot;: &quot;app&quot;,
    &quot;test2&quot;: [
      {
        &quot;status&quot;: [
          {
            &quot;id&quot;: 1,
            &quot;pole&quot;: [
              {
                &quot;Color&quot;: &quot;RED&quot;,
                &quot;Status&quot;: 0
              },
              {
                &quot;Color&quot;: &quot;AMBER&quot;,
                &quot;Status&quot;: 0
              },
              {
                &quot;Color&quot;: &quot;GREEN&quot;,
                &quot;Status&quot;: 0
              }
            ]
          },
          {
            &quot;id&quot;: 2,
            &quot;pole&quot;: [
              {
                &quot;Color&quot;: &quot;RED&quot;,
                &quot;Status&quot;: 0
              },
              {
                &quot;Color&quot;: &quot;AMBER&quot;,
                &quot;Status&quot;: 0
              },
              {
                &quot;Color&quot;: &quot;GREEN&quot;,
                &quot;Status&quot;: 0
              }
            ]
          }
        ]
      },
      {
        &quot;status&quot;: [
          {
            &quot;id&quot;: 1,
            &quot;pole&quot;: [
              {
                &quot;Color&quot;: &quot;RED&quot;,
                &quot;Status&quot;: 0
              },
              {
                &quot;Color&quot;: &quot;AMBER&quot;,
                &quot;Status&quot;: 0
              },
              {
                &quot;Color&quot;: &quot;GREEN&quot;,
                &quot;Status&quot;: 0
              }
            ]
          },
          {
            &quot;id&quot;: 2,
            &quot;pole&quot;: [
              {
                &quot;Color&quot;: &quot;RED&quot;,
                &quot;Status&quot;: 0
              },
              {
                &quot;Color&quot;: &quot;AMBER&quot;,
                &quot;Status&quot;: 0
              },
              {
                &quot;Color&quot;: &quot;GREEN&quot;,
                &quot;Status&quot;: 0
              }
            ]
          }
        ]
      }
    ]
  }
  </code></pre>
  <p>Ouput1:
  One to many output should be produced where by iterating many output of the below structure need to be produced.Like the below output it should produce 12 JSON Object.</p>
  <pre><code>{
    &quot;OUTPUT&quot;: {
      &quot;STATUS&quot;: {
        &quot;id&quot;: &quot;1&quot;,
        &quot;color&quot;: &quot;RED&quot;
      }
    }
  }
  </code></pre>
  <p>Output2:</p>
  <pre><code>{
        &quot;OUTPUT&quot;: {
          &quot;STATUS&quot;: [{
            &quot;id&quot;: &quot;1&quot;,
            &quot;color&quot;: &quot;RED&quot;
          },{
            &quot;id&quot;: &quot;1&quot;,
            &quot;color&quot;: &quot;AMBER&quot;
          }
          .
          .
          {
            &quot;id&quot;: &quot;2&quot;,
            &quot;color&quot;: &quot;RED&quot;
          }
        ]
        }
      }
  </code></pre>
  <p>As I'm new to groovy I'm not getting clearly how I can do this.</p>
  <p>Below is the code which I have tried so, far.</p>
  <pre><code>import groovy.json.*;
  
  def data='''{&quot;name&quot;: &quot;app&quot;,&quot;test2&quot;: [{&quot;status&quot;: [{&quot;id&quot;: 1,&quot;pole&quot;: [{&quot;Color&quot;: &quot;RED&quot;,&quot;Status&quot;: 0},{&quot;Color&quot;: &quot;AMBER&quot;,&quot;Status&quot;: 0},{&quot;Color&quot;: &quot;GREEN&quot;,&quot;Status&quot;: 0}]},{&quot;id&quot;: 2,&quot;pole&quot;: [{&quot;Color&quot;: &quot;RED&quot;,&quot;Status&quot;: 0},{&quot;Color&quot;: &quot;AMBER&quot;,&quot;Status&quot;: 0},{&quot;Color&quot;: &quot;GREEN&quot;,&quot;Status&quot;: 0}]}]},{&quot;status&quot;: [{&quot;id&quot;: 1,&quot;pole&quot;: [{&quot;Color&quot;: &quot;RED&quot;,&quot;Status&quot;: 0},{&quot;Color&quot;: &quot;AMBER&quot;,&quot;Status&quot;: 0},{&quot;Color&quot;: &quot;GREEN&quot;,&quot;Status&quot;: 0}]},{&quot;id&quot;: 2,&quot;pole&quot;: [{&quot;Color&quot;: &quot;RED&quot;,&quot;Status&quot;: 0},{&quot;Color&quot;: &quot;AMBER&quot;,&quot;Status&quot;: 0},{&quot;Color&quot;: &quot;GREEN&quot;,&quot;Status&quot;: 0}]}]}]}'''
  
  
  def data1 = new JsonSlurper().parseText( data );
  
  def a=[:]
  def OU=[:]
  def status=[]
  a.OUTPUT=OU
  OU.STATUS=status
  for(def i=0;i&lt;data1.test2.size();i++){
  for(def j=0;j&lt;data1.test2[i].status.size();j++){
      def b=[:]
     b.id=data1.test2[i].status[j].id
     for(def k=0;k&lt;data1.test2[i].status[j].pole.size();k++){
         b.color=data1.test2[i].status[j].pole[k].Color
         status&lt;&lt;b
     }
  }
  }
  
  
  
  
  
  String finalJson = JsonOutput.toJson a;
  println finalJson
  </code></pre>
  <p>Is this the correct way forming json(using map) as I found apart from JSONBuilder, Map is also another way for creating JSON. Earlier I was using org.json for creating dynamic json normlization script so, if the above method which I used is correct then I can proceed with generating dynamic data normalization from JSON-JSON with the groovy script from java code.</p>