Search code examples
jsonscalalift-json

Having problems parsing JSON object - JField(xyz,JDouble(90.21)) and accessing the value "xyz"


I have parsed a response from an HTTP API using the parse() function from the Lift library

val resultObj = parse(response)
val dps = resultObj \\ "dps"
println("dps are "+dps)

I have something like this now :

JObject(List(JField(1410418778,JDouble(0.0)), JField(1410418947,JDouble(0.0)),       JField(1410419163,JDouble(0.0)), JField(1410419314,JDouble(0.0)))

I want to retreive "1410418778" and the corresponding double value i.e 0.0 out of this.

I have tried the following :

dps.children.foreach(element=>{
  println("element "+element+ "and its extract is Double "+
    element.extract[Double]+" and its String extract is "+
    element.extract[String])
  val child = element.children
  println("element child "+child)
})

output ::

element JField(1410420437,JDouble(1.0))and its extract is Double 1.0 and its String extract is 1.0
element child List(JDouble(1.0))

Hoever both extract[String] and extract[Double] are giving only the value in the JDouble() field. Hw do i extract the string timestamp out of this too? Thanks in advance!


Solution

  • Was unnecessarily complicating the answer. It is as direct as this :

    val resultMap : Map[String , Any] = dps.asInstanceOf[JObject].values
        println(resultMap)
    

    Got from here :

    Extract Json values as Map with lift-json