title: Kotlin: Check if JSONObject key exists
body: <p><strong>I want to check if an entry in my <code>JSONObject</code> exists and is the type it should be to avoid an exception when used.</strong></p>
  
  <p>I found my question almost has an answer for Java <a href="https://stackoverflow.com/q/32470315/391691">Check if JSONObject key exists</a> to use the <code>has()</code> operator</p>
  
  <pre><code>import org.json.JSONArray
  import org.json.JSONObject
  
  var j:JSONObject
  
          ...
  
  val x:Int = if (j.has("mykey"){
     j["mykey"] as Int
  }else {
     0
  }
  </code></pre>
  
  <p>If <code>val x:Int = j["mykey"] as Int</code> is executed without the check it will throw an exception.</p>
  
  <p>Is there a more <em>kotlin</em>ish way, maybe with some Elvis operator or something, of doing this check?</p>
  
  <p>What is good way to get get the type of <code>j["mykey"]</code> without risking triggering an exception?</p>