title: Convert spark dataframe map column to json
body: <p>I have a dataframe with below schema and sample record</p>
  
  <pre><code>root
   |-- name: string (nullable = true)
   |-- matches: map (nullable = true)
   |    |-- key: string
   |    |-- value: integer (valueContainsNull = false)
  
  +---------------+------------------------------------------------------------------------------------------+
  |name           |matches                                                                                   |
  +---------------+------------------------------------------------------------------------------------------+
  |CVS_Extra      |Map(MLauer -&gt; 1, MichaelBColeman -&gt; 1, OhioFoodbanks -&gt; 1, 700wlw -&gt; 1, cityofdayton -&gt; 1)|
  </code></pre>
  
  <p>I am trying to convert map type column to json using below code(json4s library):</p>
  
  <pre><code>val d = countDF.map( row =&gt; (row(0),convertMapToJSON(row(1).asInstanceOf[Map[String, Int]]).toString()))
  </code></pre>
  
  <p>But fails with</p>
  
  <pre><code>java.lang.ClassNotFoundException: scala.Any
      at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
      at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
      at java.lang.Class.forName0(Native Method)
      at java.lang.Class.forName(Class.java:348)
      at scala.reflect.runtime.JavaMirrors$JavaMirror.javaClass(JavaMirrors.scala:555)
      at scala.reflect.runtime.JavaMirrors$JavaMirror$$anonfun$classToJava$1.apply(JavaMirrors.scala:1210)
      at scala.reflect.runtime.JavaMirrors$JavaMirror$$anonfun$classToJava$1.apply(JavaMirrors.scala:1202)
      at scala.reflect.runtime.TwoWayCaches$TwoWayCache$$anonfun$toJava$1.apply(TwoWayCaches.scala:50)
      at scala.reflect.runtime.Gil$class.gilSynchronized(Gil.scala:19)
      at scala.reflect.runtime.JavaUniverse.gilSynchronized(JavaUniverse.scala:16)
      at scala.reflect.runtime.TwoWayCaches$TwoWayCache.toJava(TwoWayCaches.scala:45)
      at scala.reflect.runtime.JavaMirrors$JavaMirror.classToJava(JavaMirrors.scala:1202)
      at scala.reflect.runtime.JavaMirrors$JavaMirror.runtimeClass(JavaMirrors.scala:194)
      at scala.reflect.runtime.JavaMirrors$JavaMirror.runtimeClass(JavaMirrors.scala:54)
      at org.apache.spark.sql.catalyst.ScalaReflection$.getClassFromType(ScalaReflection.scala:682)
      at org.apache.spark.sql.catalyst.ScalaReflection$.org$apache$spark$sql$catalyst$ScalaReflection$$dataTypeFor(ScalaReflection.scala:84)
      at org.apache.spark.sql.catalyst.ScalaReflection$$anonfun$10.apply(ScalaReflection.scala:614)
      at org.apache.spark.sql.catalyst.ScalaReflection$$anonfun$10.apply(ScalaReflection.scala:607)
      at scala.collection.TraversableLike$$anonfun$flatMap$1.apply(TraversableLike.scala:241)
      at scala.collection.TraversableLike$$anonfun$flatMap$1.apply(TraversableLike.scala:241)
      at scala.collection.immutable.List.foreach(List.scala:381)
      at scala.collection.TraversableLike$class.flatMap(TraversableLike.scala:241)
      at scala.collection.immutable.List.flatMap(List.scala:344)
      at org.apache.spark.sql.catalyst.ScalaReflection$.org$apache$spark$sql$catalyst$ScalaReflection$$serializerFor(ScalaReflection.scala:607)
      at org.apache.spark.sql.catalyst.ScalaReflection$.serializerFor(ScalaReflection.scala:438)
      at org.apache.spark.sql.catalyst.encoders.ExpressionEncoder$.apply(ExpressionEncoder.scala:71)
      at org.apache.spark.sql.Encoders$.product(Encoders.scala:275)
      at org.apache.spark.sql.LowPrioritySQLImplicits$class.newProductEncoder(SQLImplicits.scala:233)
      at org.apache.spark.sql.SQLImplicits.newProductEncoder(SQLImplicits.scala:33)
  
  Scala Version - 2.11, json4s-jackson_2.11  &amp; spark 2.2.0
  </code></pre>
  
  <p>Can anyone please suggest how to overcome this error. Thanks in advance.</p>