15

contains like functionality for play.api.libs.json.Json

val data=Map("id" -> "240190", "password" -> "password","email" -> "email@domain.com")

data.contains("email")//true


val info=Json.obj("id" -> "240190", "password" -> "password","email" -> "email@domain.com")

now how to check info contains email or not?

Govind Singh
  • 15,282
  • 14
  • 72
  • 106

3 Answers3

23
info.keys.contains("email")

The .keys gives you back a Set with the key values and then you can call the contains method, I'm not sure there's a more direct way to do it.

Ende Neu
  • 15,581
  • 5
  • 57
  • 68
7
(info \ "email").asOpt[String].isEmpty

as asOpt would return Optional, we can have isEmpty simple check, this would do what we want.

Mahesh Pujari
  • 493
  • 1
  • 9
  • 15
6
(info \ "email").asOpt[String] match {
  case Some(data) => println("here is the value for the key email represented by variable data" + data)
  case None => println("email key is not found") 
}
dk14
  • 22,206
  • 4
  • 51
  • 88
Sangeeta
  • 491
  • 5
  • 22
  • This won't work when you do things like `.asOpt[Boolean]`, which will always give you a Some(false) when the key doesn't exist. – chuchao333 Jul 24 '17 at 17:19