title: How to fetch JSON object from Json array in REST API
body: <p>I am new to REST API and handling JSON in our automation script. I have an API whose response is <code>JSONArray</code> i.e.,</p>
  
  <pre><code>[{"ProjectID":15,"ProjectName":" Securities"},{"ProjectID":16,"ProjectName":"PAS "}]
  </code></pre>
  
  <p>While automation, for verification I need to fetch the reponse. I have tried the below one but not getting expected output</p>
  
  <pre><code> import java.io.BufferedReader;
   import java.io.IOException;
   import java.io.InputStreamReader;
   import java.net.HttpURLConnection;
   import java.net.MalformedURLException;
   import java.net.URL;
   import java.util.Scanner;
   import org.json.*;
  
   public class ProjectNameVerfication {
  
   public static void main(String[] args) throws JSONException
   {
  
    try 
    {
      URL url = new URL("http://17*.**.**.**:3000/api/******");
      HttpURLConnection conn = (HttpURLConnection) url.openConnection();
      conn.setRequestMethod("GET");
      conn.setRequestProperty("Accept", "application/json");
  
      if (conn.getResponseCode() != 200) 
      {
         throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
      }
  
      Scanner scan = new Scanner(url.openStream());
      String str = new String();
      while (scan.hasNext())
         str += scan.nextLine();
      scan.close();
  
      System.out.println("str : " + str);
  
      JSONObject obj = new JSONObject(str.substring(str.indexOf('{')));
      System.out.println("obj : " +obj);
      int ProjectID = obj.optInt("ProjectID");
      String ProjectName = obj.getString("ProjectName");
  
      System.out.println("ProjectID: " +ProjectID);
      System.out.println("ProjectName: " +ProjectName);
  
      JSONArray arr = obj.getJSONArray("ProjectID");
      for (int i = 0; i &lt; arr.length(); i++)
      {
        String post_id =arr.getJSONObject(i).getString("ProjectName");
      }
  
      conn.disconnect();
    }
    catch (MalformedURLException e) { e.printStackTrace();}
    catch (IOException e) { e.printStackTrace(); }
  
   }
  }
  </code></pre>
  
  <p>Actual output is bellow:</p>
  
  <pre><code>str : [{"ProjectID":15,"ProjectName":" Securities"},{"ProjectID":16,"ProjectName":"PAS "}]
  
  obj : {"ProjectName":" securities""ProjectID":15}
  
  ProjectID: 15
  ProjectName:  Securities
  
  Exception in thread "main" org.json.JSONException: JSONObject["ProjectID"] is not a JSONArray.
      at org.json.JSONObject.getJSONArray(JSONObject.java:539)
      at MyTest.ProjectNameVerfication.main(ProjectNameVerfication.java:60)
  </code></pre>