title: Fetch JSON values in java
body: <p>API used for the json parsing and iteration: </p>
  
  <ul>
  <li>org.json.simple</li>
  <li>org.json</li>
  </ul>
  
  <p>I am trying to fetch both the values from FieldName under ArchiveBatchChangeRequest -> DocumentSets -> QuerySpecification and return it as an ArrayList.</p>
  
  <ul>
  <li>elementNode =
  "ArchiveBatchChangeRequest;DocumentSets;QuerySpecification"</li>
  <li>objectValue = "FieldName"</li>
  <li>batchFile = JSON input file</li>
  </ul>
  
  <p>So the return value should contain CustomerName and DateCreated because QuerySpecification is an array with a size of 2</p>
  
  <p>I just can't seem to get this working. I am afraid my initial point of the traversing is pretty bad or could be done much easier.</p>
  
  <p><strong>The JSON input file:</strong></p>
  
  <pre><code>{
      "ArchiveBatchChangeRequest": [{
          "BatchRunSpecification": [{
              "BatchOperation": "EXTRACT"
          }]
      },
      {
          "OutputSpecification": [{
                  "ReportFieldNames": ["ReportField1"]
          }]  
      },
      {
          "DocumentSets": [{
              "DocumentSetNo": "1",
              "QuerySpecification": [{
                  "FieldName": "CustomerName",
                  "SimpleQuery": [{
                      "Operator": "EQUAL",
                      "Values": ["Customer1"]
                  }]
              },
              {
                  "FieldName": "DateCreated",
                  "SimpleQuery": [{
                      "Operator": "EQUAL",
                      "Values": ["19-12-2015"]
                  }]
              }]
          }]
      }]
  }
  </code></pre>
  
  <p><strong>The code:</strong></p>
  
  <pre><code>        public ArrayList&lt;String&gt; getElementValue(String elementNode, String objectValue, File batchFile) throws IOException, ParseException{
              String [] elementArray = elementNode.split(";");
              JSONObject outerObject = null;
              ArrayList&lt;String&gt; values = new ArrayList&lt;String&gt;();
              try {
                  JSONParser parser = new JSONParser();
                      Object object = parser.parse(new FileReader(batchFile));
                  outerObject = new JSONObject(object.toString());
              } catch (JSONException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              } catch (FileNotFoundException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
  
              JSONArray getArray = null;
  
              for (int i = 0; i &lt; elementArray.length; i++) {
                  getArray = outerObject.getJSONArray(elementArray[i]);
  
                  for (int j = 0; j &lt; getArray.length(); j++) {
                      for (int j2 = 0; j2 &lt; elementArray.length; j2++) {
                          if(getArray.getJSONObject(j).names().get(0).equals(elementArray[j2])){
                              outerObject = getArray.getJSONObject(j);
                          }
                      }
                  }
  
                  if(i == elementArray.length - 1){
                      outerObject = getArray.getJSONObject(0);
                  }
              }
  
              try{
                  getArray = null;
                  getArray = outerObject.getJSONArray(objectValue);
              }catch(JSONException je){ //Object is not an JSONArray
                  je.printStackTrace();
              }finally{
                  if(getArray != null){
                      for (int i = 0; i &lt; getArray.length(); i++) {
                          values.add(getArray.getString(i).replace("\"", ""));
                      }
                  }else 
                      values.add(outerObject.getString(objectValue));
              }
  
  
              return values;
          }
  </code></pre>