title: JAVA: is the recursive approache the best way to check if json contains a specific key with a specific data value?
body: <p>I am looking to test if my json input contains a specific key with a specific value. I used org.joson lib to parse it and test it. I followed a recursive approach.
  Is it the best approach to use? or is there a simpler one?</p>
  
  <pre><code>public boolean isJsonContains(String dataKey, String dataValue) {
      logger.systemWriteOutput("is JSON data key "+ dataKey + " contains value: " +dataValue,3);  
      JSONObject jsonObject = null;
  
      if (jsonInput!=null){
          jsonObject = new JSONObject(jsonInput);
          return isJSONObjectContainsKeyAndValue(dataKey, dataValue,jsonObject );
      }
      else{
          return false;
      }       
  }
  
  private boolean isJSONObjectContainsKeyAndValue (String dataKey,  String dataValue, JSONObject object){
  
      String[] keys = JSONObject.getNames(object);
      logger.systemWriteOutput("JSONObject is: "+object.toString(),3);
  
      for (String key : keys)
      {                   
          boolean result = isJSONKeyContainsValue(key,  dataValue, object);
          if(result)
              return true;
      }
      logger.systemWriteOutput("no value : "+ dataValue +" has been found for id: " +dataKey,3);
      return false;   
  }
  
  private boolean isJSONKeyContainsValue (String dataKey,  String dataValue, JSONObject object){
  
      Object value = object.get(dataKey);         
      if(dataKey.equals(dataKey) &amp;&amp; value.toString().equals(dataValue)){
          logger.systemWriteOutput("value of id: " +dataKey+" is: "+value.toString(),3);
          return true;
      }
      else{
          logger.systemWriteOutput("value of id: " +dataKey+" is: "+value.toString(),3);
          //see if that the nested element contains the id
          if (value instanceof JSONArray){
              logger.systemWriteOutput("key array is: "+dataKey,3);
              JSONArray jsonArray = (JSONArray) object.getJSONArray(dataKey);
              logger.systemWriteOutput("key array lenbgth is: "+jsonArray.length(),3);
              return isJSONArrayContainsKeyAndValue(dataKey,dataValue, jsonArray);
          }
          else  if (value instanceof JSONObject){
              return isJSONObjectContainsKeyAndValue(dataKey, dataValue, object.getJSONObject(dataKey));
          }
      }
      return false;
  }
  
  private boolean isJSONArrayContainsKeyAndValue(String dataKey, String dataValue, JSONArray jsonArray) {
      boolean result;
  
      logger.systemWriteOutput("jsonArray is: "+jsonArray.toString(),3);
      logger.systemWriteOutput("jsonArray length is: "+jsonArray.length(),3);
  
      for (int i = 0; i &lt; jsonArray.length(); i++) {
  
          JSONObject jsonobject = jsonArray.getJSONObject(i);
          result= isJSONObjectContainsKeyAndValue(dataKey, dataValue ,jsonobject);
          if( result)
              return result;
      }
      return false;
  }
  </code></pre>
  
  <p>Thanks in advance for your help!</p>