You'd have to use pattern matching/extractors:

val aOpt: List[String] = for {
  JObject(map) <- parse(body)
  JField("a", JString(value)) <- map
} yield value

alternatively use querying DSL

parse(body) \ "a" match {
  case JString(value) => Some(value)
  case _              => None

These are options as you have no guarantee that arbitrary JSON would contain field "a".

See documentation

extract would make sense if you were extracting whole JObject into a case class.


To use extract you need to create a class that matches the shape of the JSON that you are parsing. Here is an example using your input data:

val body ="""
  "a": "hello",
  "b": "goodbye"

case class Body(a: String, b: String)

import org.json4s._
import org.json4s.jackson.JsonMethods._

implicit val formats = DefaultFormats

val b = Extraction.extract[Body](parse(body))

println(b.a) // hello

Related Query

More Query from same tag