title: Convert Array[Byte] to JSON format using Spark Scala
body: <p>I'm reading an .avro file where the data of a particular column is in binary format. I'm currently converting the binary format to string format with the help of UDF for a readable purpose and then finally i will need to convert it into JSON format for further parsing the data. Is there a way i can convert string object to JSON format using Spark Scala code. </p>
  
  <p>Any help would be much appreciated. </p>
  
  <pre><code>val avroDF = spark.read.format("com.databricks.spark.avro").
  load("file:///C:/46.avro")
  
  import org.apache.spark.sql.functions.udf
  
  // Convert byte object to String format
  
  val toStringDF = udf((x: Array[Byte]) =&gt; new String(x))
  
  
  val newDF = avroDF.withColumn("BODY", 
  toStringDF(avroDF("body"))).select("BODY")
  </code></pre>
  
  <p>Output of newDF is shown below: </p>
  
  <pre><code>BODY                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
  +---------------------------------------------------------------------------------------------------------------+
  |{"VIN":"FU74HZ501740XXXXX","MSG_TYPE":"SIGNAL","TT":0,"RPM":[{"E":1566800008672,"V":1073.75},{"E":1566800002538,"V":1003.625},{"E":1566800004084,"V":1121.75}
  </code></pre>
  
  <p>My desired output should be like below:
  <a href="https://i.stack.imgur.com/hUiz2.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hUiz2.png" alt="enter image description here"></a></p>