Search code examples
scalajson4s

Getting a field in Scala json4s


How do I get a particular field out of a Json object in Scala? I feel like I'm going in circles.

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

val me = parse(""" {"name":"brian", "state":"frustrated"} """)

Now I want just the state. I was looking for something like

me("state") -> "frustrated"

I have tried

me("state")
me.get("state")
me \ "state" <thanks for the idea>
me['state']
me.state
me.NOOOOOOOOOO!!!!!!!

Help?


Solution

  • I think your code has errta, and below may be right code.

    Assume type of value in state field is fixed, say its type is string.

    val me = parse("""{"name":"brian", "state":"frustrated"}""")
    val JString(state) = me \ "state"