title: How to convert Row to json in Spark 2 Scala
body: <p>Is there a simple way to converting a given Row object to json?</p>
  
  <p>Found this about converting a whole Dataframe to json output:
  <a href="https://stackoverflow.com/questions/36157810/spark-row-to-json">Spark Row to JSON</a></p>
  
  <p>But I just want to convert a one Row to json. 
  Here is pseudo code for what I am trying to do.</p>
  
  <p>More precisely I am reading json as input in a Dataframe.
  I am producing a new output that is mainly based on columns, but with one json field for all the info that does not fit into the columns.</p>
  
  <p>My question what is the easiest way to write this function: convertRowToJson()</p>
  
  <pre><code>def convertRowToJson(row: Row): String = ???
  
  def transformVenueTry(row: Row): Try[Venue] = {
    Try({
      val name = row.getString(row.fieldIndex("name"))
      val metadataRow = row.getStruct(row.fieldIndex("meta"))
      val score: Double = calcScore(row)
      val combinedRow: Row = metadataRow ++ ("score" -&gt; score)
      val jsonString: String = convertRowToJson(combinedRow)
      Venue(name = name, json = jsonString)
    })
  }
  </code></pre>
  
  <p>Psidom's Solutions:</p>
  
  <pre><code>def convertRowToJSON(row: Row): String = {
      val m = row.getValuesMap(row.schema.fieldNames)
      JSONObject(m).toString()
  }
  </code></pre>
  
  <p>only works if the Row only has one level not with nested Row. This is the schema: </p>
  
  <pre><code>StructType(
      StructField(indicator,StringType,true),   
      StructField(range,
      StructType(
          StructField(currency_code,StringType,true),
          StructField(maxrate,LongType,true), 
          StructField(minrate,LongType,true)),true))
  </code></pre>
  
  <p>Also tried Artem suggestion, but that did not compile:</p>
  
  <pre><code>def row2DataFrame(row: Row, sqlContext: SQLContext): DataFrame = {
    val sparkContext = sqlContext.sparkContext
    import sparkContext._
    import sqlContext.implicits._
    import sqlContext._
    val rowRDD: RDD[Row] = sqlContext.sparkContext.makeRDD(row :: Nil)
    val dataFrame = rowRDD.toDF() //XXX does not compile
    dataFrame
  }
  </code></pre>