+2  A: 

If you look at the implementation, you'll see

case class JObject(obj: List[JField]) extends JValue {
  type Values = Map[String, Any]
  def values = Map() ++ obj.map(_.values.asInstanceOf[(String, Any)]) // FIXME compiler fails if cast is removed
}

So this should work:

record.values.asInstanceOf[Map[String, Any]]("foo")

You could also try

record.values.apply("foo")
Alexey Romanov
Thanks, the first one works, the second option of using apply() returns an error. I ended using scala's builtin java parser. scala.util.parsing.json.JSON.parseFull(record) will return a Some(Map) or Some(List)
tommy chheng
+2  A: 

JValue.Values is a path dependent type. Meaning that if you hold on a JString it will be a String, or if you got a JArray it will be a List[Any]. If you are sure that the JSON you parse is a JSON object you can cast it to a proper type.

val record = JsonParser.parse(json).asInstanceOf[JObject]

A path dependent type for JObject is a Map[String, Any], thus:

scala> record.values("foo")                                     
res0: Any = bar

Just of curiosity, isn't it a bit problematic if you do not know the shape of data you are going to parse?

Note, if your data contains (name, description, age) and the age is optional you can read that JSON into:

case class Person(name: String, description: String, age: Option[Int])
Joni
I have a json which has an array of different fields. For ex. the first document may have (name, description, age) but the second document may only have (name, age) specified. If i use scala's Map object, i can just call document.getOrElse("foo", "")
tommy chheng