How do I detect if a Spark DataFrame has a column
<p>When I create a <code>DataFrame</code> from a JSON file in Spark SQL, how can I tell if a given column exists before calling <code>.select</code></p>

<p>Example JSON schema:</p>

<pre><code>{
  "a": {
    "b": 1,
    "c": 2
  }
}
</code></pre>

<p>This is what I want to do:</p>

<pre class="lang-scala prettyprint-override"><code>potential_columns = Seq("b", "c", "d")
df = sqlContext.read.json(filename)
potential_columns.map(column =&gt; if(df.hasColumn(column)) df.select(s"a.$column"))
</code></pre>

<p>but I can't find a good function for <code>hasColumn</code>. The closest I've gotten is to test if the column is in this somewhat awkward array: </p>

<pre><code>scala&gt; df.select("a.*").columns
res17: Array[String] = Array(b, c)
</code></pre>