简体   繁体   中英

JsPath to array in Scala Play Json

import play.api.libs.json._

val json: JsValue = Json.parse("""
  {
    "name" : "Independence",
    "foundingFathers" : [ {
      "name" : {
        "first": "John",
        "last": "Adams"
      },
      "country" : "United States"
    }, {
      "name" : {
        "first": "Artur",
        "last": "Mas"
      },
      "country" : "Catalonia"
    } ]
  }
""")

val lastNames = json \ "foundingFathers \ "name" \ "last"
// this does not work. it fails with: JsUndefined('last' is undefined on object: JsUndefined('name' is undefined on object: [{"name":{"first":"John","last":"Adams"},"country":"United States"},{"name":{"first":"Artur","last":"Mas"},"country":"Catalonia"}]))

// this does work, but there is too much boilerplate
val lastNames = (json \ "foundingFathers").as[List[JsObject]].map(_ \ "name" \ "last")

how to achieve the result without that boilerplate? (in this case I want to use JsValues, I don't want to use a reader to convert JsValue to a model)

You can use implicit classes for wrap it with this functionality

implicit class JsValueWrapper(val j: JsValue) extends AnyVal {
  def \|(fieldName: String) = j match {
    case JsArray(value) => JsArray(value.map(_ \ fieldName))
    case _ => j \ fieldName
  }
}

scala> json \| "foundingFathers" \| "name" \| "first"
res19: play.api.libs.json.JsValue = ["John","Artur"]

minor improvement, not sure whether it answers your question

val lastNames = json \ "foundingFathers" \\ "name" map ( _ \ "last")

lastNames: Seq[play.api.libs.json.JsValue] = ListBuffer("Adams", "Mas")

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM