title: Scala Play JSON Type conversion
body: <p>I'm currently struggling with the type conversion of the Play! Json library for Scala...<br>
  I have data that looks like this:    </p>
  
  <pre><code>val myJson = Json.parse(...)
  
  val mapping = Map[String, Option(JsValue)](
      "id" -&gt; (myJson \ "Path" \ stringValue),
      "num" -&gt; (myJson \ "Path" \ intValue),
      "precision" -&gt; (myJson \ "Path" \ "floatValue")
  )
  </code></pre>
  
  <p>Also, I have an Avro Schema that defines the data types of each field, in this case it's:<br>
  - id: String<br>
  - num: Int<br>
  - precision: Float   </p>
  
  <p>If I now iterate over the fields and check the types against the Avro Schema like this (Note that field in this case is the name of the value to get, e.g. "id"):</p>
  
  <pre><code>    ...
      case Schema.Type.STRING =&gt; mapping.getOrElse(field, None) match {
        case Some(stringVal: JsValue) =&gt; stringVal.as[String]
        case None =&gt; null
      }
      case Schema.Type.INT =&gt; mapping.getOrElse(field, None) match {
        case Some(intVal) =&gt; intVal.as[Int]
        case None =&gt; null
      }
      case Schema.Type.FLOAT =&gt; mapping.getOrElse(field, None) match {
        case Some(floatVal) =&gt; floatVal.as[Float]
        case None =&gt; null
      }
      ...
  </code></pre>
  
  <p>I always get this Error:   </p>
  
  <pre><code>[org.apache.kafka.streams.processor.internals.StreamThread]  - Streams application error during processing in thread [StreamThread-1]:play.api.libs.json.JsResultException: JsResultException(errors:List((,List(JsonValidationError(List(error.expected.jsnumber),WrappedArray())))))
  </code></pre>
  
  <p>(Note: of course the error.expected.jsnumber can also be jsstring sometimes)</p>
  
  <p>Here are the ways of conversion that I already tried, and did not work (I have several different of the mappings that in the end still contain either None or a value of given type):   </p>
  
  <pre><code>floatVal.as[Float]
  floatVal.as[JsNumber].as[Float]
  floatVal.as[JsNumber].value.toFloat
  Hi,
  </code></pre>
  
  <p>I'm currently struggling with the type conversion of the Play! Json library for Scala...<br>
  I have data that looks like this:    </p>
  
  <pre><code>val myJson = Json.parse(...)
  
  val mapping = Map[String, Option(JsValue)](
      "id" -&gt; (myJson \ "Path" \ stringValue),
      "num" -&gt; (myJson \ "Path" \ intValue),
      "precision" -&gt; (myJson \ "Path" \ "floatValue")
  )
  </code></pre>
  
  <p>Also, I have an Avro Schema that defines the data types of each field, in this case it's:<br>
  - id: String<br>
  - num: Int<br>
  - precision: Float   </p>
  
  <p>If I now iterate over the fields and check the types against the Avro Schema like this (Note that field in this case is the name of the value to get, e.g. "id"):</p>
  
  <pre><code>    ...
      case Schema.Type.STRING =&gt; mapping.getOrElse(field, None) match {
        case Some(stringVal: JsValue) =&gt; stringVal.as[String]
        case None =&gt; null
      }
      case Schema.Type.INT =&gt; mapping.getOrElse(field, None) match {
        case Some(intVal) =&gt; intVal.as[Int]
        case None =&gt; null
      }
      case Schema.Type.FLOAT =&gt; mapping.getOrElse(field, None) match {
        case Some(floatVal) =&gt; floatVal.as[Float]
        case None =&gt; null
      }
      ...
  </code></pre>
  
  <p>I always get this Error:   </p>
  
  <pre><code>[org.apache.kafka.streams.processor.internals.StreamThread]  - Streams application error during processing in thread [StreamThread-1]:play.api.libs.json.JsResultException: JsResultException(errors:List((,List(JsonValidationError(List(error.expected.jsnumber),WrappedArray())))))
  </code></pre>
  
  <p>(Note: of course the error.expected.jsnumber can also be jsstring sometimes)</p>
  
  <p>Here are the ways of conversion that I already tried, and did not work (I have several different of the mappings that in the end still contain either None or a value of given type):   </p>
  
  <pre><code>floatVal.as[Float]
  floatVal.as[JsNumber].as[Float]
  floatVal.as[JsNumber].value.toFloat
  floatVal.as[String].value.toFloat
  floatVal.as[JsString].value.toString.toFloat
  floatVal.as[String].toFloat
  floatVal.as[String].value.toFloat
  floatVal.as[JsString].value.toString.toFloat
  floatVal.as[JsString].as[String].toFloat
  floatVal.as[JsString].as[String].value.toFloat
  floatVal.as[JsString].as[String].value.toString.toFloat
  </code></pre>
  
  <p>Somehow I have problems for all the types, only in some cases, but I have them...<br>
  What is also interesting is, that even though the type is <code>JsValue</code>, when I look at them with the Debugger, the type says <code>Js&lt;type&gt;</code>, e.g. <code>JsString</code> instead of <code>JsValue</code>.<br>
  In some occasions, however not always, it also displays <code>JsString</code> for the Int type...   </p>
  
  <p>Why doesn't it keep them as <code>JsValue</code>?</p>
  
  <p>What is the right way to convert the Play! Json types?</p>
  
  <p>Thank you
  All the best
  Tim </p>