Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a key exists in play.api.libs.json.Json

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

val data=Map("id" -> "240190", "password" -> "password","email" -> "[email protected]")

data.contains("email")//true


val info=Json.obj("id" -> "240190", "password" -> "password","email" -> "[email protected]")

now how to check info contains email or not?

like image 925
Govind Singh Avatar asked Jul 23 '14 07:07

Govind Singh


People also ask

How do you check if a JSON object contains a key or not?

Return valueJsonObject::containsKey() returns a bool that tells whether the key was found or not: true if the key is present in the object. false if the key is absent of the object.

What is a JSValue?

You use the JSValue class to convert basic values, such as numbers and strings, between JavaScript and Objective-C or Swift representations to pass data between native code and JavaScript code.

What is Play JSON?

The Play JSON API provides implicit Writes for most basic types, such as Int , Double , String , and Boolean . It also supports Writes for collections of any type T that a Writes[T] exists. import play. json.


3 Answers

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.

like image 175
Ende Neu Avatar answered Oct 01 '22 18:10

Ende Neu


(info \ "email").asOpt[String].isEmpty

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

like image 20
Mahesh Pujari Avatar answered Oct 01 '22 19:10

Mahesh Pujari


(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") 
}
like image 39
Sangeeta Avatar answered Oct 01 '22 17:10

Sangeeta