title: how to iterate json and convert into following json format in JAVA
body: <p>How to iterate json in java. I wanted iterate elements inside payload, grab the first level and second level key and array value. I have converted the same in python. I'm new to java and wanted convert in java. Any help would be appreciated.</p>
  <p><strong>Python Code:</strong></p>
  <pre><code>import os
  import sys
  import json
  import datetime
  def lambda_handler(event, context):
      indata = event['payload']
      outputDictionary = []
      for i in indata:
          for j in indata[i]:
              for k in indata[i][j]:
                  outputDictionary.append(dict(source = i,code=j,version=k))
      return outputDictionary 
  </code></pre>
  <p><strong>Input :</strong></p>
  <pre><code>{
      &quot;payload&quot;: {
          &quot;gtl&quot;: {
              &quot;435185&quot;: [
                  &quot;2019-11-27T14:34:32.368197Z&quot;
              ]
          },
          &quot;ptl&quot;: {
              &quot;A0947863&quot;: [
                  &quot;2019-11-27T14:34:32.368197Z&quot;
              ]
          },
          &quot;mtl&quot;: {
              &quot;A0947863&quot;: [
                  &quot;2019-11-27T14:34:32.368197Z&quot;,
                  &quot;2021-04-27T06:13:12.841968Z&quot;
              ]
          }
      }
  }
  </code></pre>
  <p><strong>Expected Output</strong></p>
  <pre><code>[
      {
          &quot;source&quot;: &quot;gtl&quot;,
          &quot;code&quot;: &quot;435185&quot;,
          &quot;version&quot;: &quot;2019-11-27T14:34:32.368197Z&quot;
      },
      {
          &quot;source&quot;: &quot;ptl&quot;,
          &quot;code&quot;: &quot;A0947863&quot;,
          &quot;version&quot;: &quot;2019-11-27T14:34:32.368197Z&quot;
      },
      {
          &quot;source&quot;: &quot;mtl&quot;,
          &quot;code&quot;: &quot;A0947863&quot;,
          &quot;version&quot;: &quot;2019-11-27T14:34:32.368197Z&quot;
      },
      {
          &quot;source&quot;: &quot;mtl&quot;,
          &quot;code&quot;: &quot;A0947863&quot;,
          &quot;version&quot;: &quot;2021-04-27T06:13:12.841968Z&quot;
      }
  ]
  </code></pre>
  <p><strong>SOLVED IN JAVA:
  Java CODE:</strong></p>
  <pre><code>import java.util.Iterator;
  
  import org.json.JSONArray;
  import org.json.JSONException;
  import org.json.JSONObject;
  
  
  public class ParseJson {
  
      public static void main(String[] args) {
  
          String inputJson = &quot;{\r\n&quot; + 
                  &quot;   \&quot;payload\&quot;: {\r\n&quot; + 
                  &quot;       \&quot;gtl\&quot;: {\r\n&quot; + 
                  &quot;           \&quot;435185\&quot;: [\r\n&quot; + 
                  &quot;               \&quot;2019-11-27T14:34:32.368197Z\&quot;\r\n&quot; + 
                  &quot;           ]\r\n&quot; + 
                  &quot;       },\r\n&quot; + 
                  &quot;       \&quot;ptl\&quot;: {\r\n&quot; + 
                  &quot;           \&quot;A0947863\&quot;: [\r\n&quot; + 
                  &quot;               \&quot;2019-11-27T14:34:32.368197Z\&quot;\r\n&quot; + 
                  &quot;           ]\r\n&quot; + 
                  &quot;       },\r\n&quot; + 
                  &quot;       \&quot;mtl\&quot;: {\r\n&quot; + 
                  &quot;           \&quot;A0947863\&quot;: [\r\n&quot; + 
                  &quot;               \&quot;2019-11-27T14:34:32.368197Z\&quot;,\r\n&quot; + 
                  &quot;               \&quot;2021-04-27T06:13:12.841968Z\&quot;\r\n&quot; + 
                  &quot;           ]\r\n&quot; + 
                  &quot;       }\r\n&quot; + 
                  &quot;   }\r\n&quot; + 
                  &quot;}&quot;;
          final JSONObject inputJSONOBject = new JSONObject(inputJson);
          JSONObject jsonChildObject = (JSONObject)inputJSONOBject.get(&quot;payload&quot;);        
          
          Iterator iterator  = jsonChildObject.keys();
          while (iterator.hasNext()) {
              String key = null;
              while(iterator.hasNext()){
                  String source = (String)iterator.next();
                  JSONObject codeChildObject = jsonChildObject.getJSONObject(source); 
                  Iterator iterator2  = codeChildObject.keys();
                  while(iterator2.hasNext()){
                      String code = (String)iterator2.next();
                      org.json.JSONArray jsonArray = codeChildObject.getJSONArray(code);
                      for(int i=0;i&lt;jsonArray.length();i++){
                          
                          System.out.println(&quot;Source:&quot; + source);
                          System.out.println(&quot;CODE:&quot; + code); 
                          System.out.println(&quot;Version: &quot;+jsonArray.get(i));
                      }
                  }
                  }
              }
          }
          
          //System.out.println(inputJSONOBject);
              
      }
  </code></pre>