Given this json:
{
"credentials": {
"b79a2ba2-lolo-lolo-lolo-lololololol": {
"description": "meaningful description",
"displayName": "git (meaningful description)",
"fullName": "credential-store/_/b79a2ba2-lolo-lolo-lolo-lololololol",
"typeName": "SSH Username with private key"
}
},
"description": "Credentials that should be available irrespective of domain specification to requirements matching.",
"displayName": "Global credentials (unrestricted)",
"fullDisplayName": "Credentials » Global credentials (unrestricted)",
"fullName": "credential-store/_",
"global": true,
"urlName": "_"
}
and this scala destination class:
case class JenkinsCredentials(uuid: String, description: String)
How can I create a Reads[JenkinsCredentials]
to extract that first object name uuid b79a2ba2-lolo-lolo-lolo-lololololol
along with the description?
Following the documentation it'd be something along the lines of this:
implicit val credsReader: Reads[JenkinsCredentials] = (
(JsPath).read[String] and
(JsPath \ "description").read[String]
)(JenkinsCredentials.apply _)
Used with (Json.parse(content) \\\\ "credentials").validate[Seq[JenkinsCredentials]
But the documentation doesn't discuss anything about extracting the names of the objects as a field used somewhere else...
EDIT: clarifying
My end state would be a Seq
of JenkinsCredentials
that are parsed from a json Object, not an array. Because of how the JSON is structured. I'd pull out the "credentials": "UUID" and "credentials":"UUID":"description" into a single object, for each potential credentials entry under "credentials"
I figured this out not using the Reads[T]
methods:
//Get the key names inside of the credentials
(json \ "credentials").as[JsObject].fields.map{ case (credId, value) =>
JenkinsCredentials(credId, (value \ "description").validate[String].get)
}
This works, but doesn't validate bits, and doesn't use a transformer.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.