0

I have an API like this in play2.3 - reactive mongo-

 def addEndUser = Action.async(parse.json) { request =>
        val cursor: Cursor[JsObject] = collectionEndUser.find(Json.obj("mobileNumber" -> "9686563240","businessUserId" ->"1")).
        sort(Json.obj("createDate" -> -1)).cursor[JsObject]
        val futureEndUserList: Future[List[JsObject]] = cursor.collect[List]()
        futureEndUserList.map { user =>
            val x:JsObject = obj(Map("endUsers" -> toJson(user) ))
                println(x)
    }
    request.body.validate[User].map { user =>

        val jsonData = Json.obj(
            "businessUserId" ->user.businessUserId,
            "userId" -> user.userId,
            "registrantId" ->user.registrantId,
            "memberId" -> "",
            "name"   -> user.name,
            "currentPoints" -> user.currentPoints,
            "email" -> user.email,
            "mobileNumber" -> user.mobileNumber,
            "mobileCountryCode" ->user.mobileCountryCode,
            "createDate" -> (new java.sql.Timestamp(new Date().getTime)).toString,
            "updateDate" -> (new java.sql.Timestamp(new Date().getTime)).toString,
            "purchasedAmtForRedemption"->user.purchasedAmtForRedemption
        )

        collectionEndUser.insert(jsonData).map { lastError =>
                 Logger.debug(s"Successfully inserted with LastError: $lastError")
                 Created
        }
    }.getOrElse(Future.successful(BadRequest("invalid json")))
}

def findEndUserByUserId(userId: String) = Action.async {
    val cursor: Cursor[JsObject] = collectionEndUser.find(Json.obj("userId" -> userId)).
    sort(Json.obj("createDate" -> -1)).cursor[JsObject]

    val futureEndUserList: Future[List[JsObject]] = cursor.collect[List]()

    //val futureEndUserJsonArray: Future[JsArray] = futureEndUserList.map { endUser =>
        //Json.arr(endUser)
    //}

    futureEndUserList.map { user =>
        Ok(toJson(Map("endUsers" -> toJson(user) )))
    }
}

This API is called as POST method to store those fields in DB. But before adding in the DB, I want to get a value from a collection and use it in one of the fields. All though println(x) is printing the object like this {"endUsers":[{"_id":{"$oid":"543f6912903ec10f48673188"},"businessUserId":"1","createDate":"2014-10-16 12:13:30.771","currentPoints":16.0,"email":"ruthvickms@gmail.com","mobileCountryCode":"+91","mobileNumber":"9686563240","name":"Ruthvick","purchasedAmtForRedemption":50.0,"updateDate":"2014-10-17 20:23:40.725","userId":"5"},{"_id":{"$oid":"543f68c0903ec10f48673187"},"businessUserId":"1","userId":"4","name":"Ruthvick","currentPoints":"0","email":"ruthvickms@gmail.com","mobileNumber":"9686563240","mobileCountryCode":"+91","createDate":"2014-10-16 12:12:08.692","updateDate":"2014-10-16 12:12:08.692","purchasedAmtForRedemption":"0"},{"_id":{"$oid":"543f689e903ec10f48673186"},"businessUserId":"1","userId":"3","name":"Ruthvick","currentPoints":"0","email":"ruthvickms@gmail.com","mobileNumber":"9686563240","mobileCountryCode":"+91","createDate":"2014-10-16 12:11:34.079","updateDate":"2014-10-16 12:11:34.079","purchasedAmtForRedemption":"0"},{"_id":{"$oid":"543f63ef903ec10f48673185"},"businessUserId":"1","userId":"2","name":"Ruthvick","currentPoints":"0","email":"ruthvickms@gmail.com","mobileNumber":"9686563240","mobileCountryCode":"+91","createDate":"2014-10-16 11:51:35.394","updateDate":"2014-10-16 11:51:35.394","purchasedAmtForRedemption":"0"}]}, parsing like this

x.endUsers[0].name is throwing error like

 identifier expected but integer literal found.
    println(x.endUsers[0].name)

Please help me to parse this..I'm a beginner in play framework.

Thanks

user3777846
  • 19
  • 1
  • 7

1 Answers1

0

I think this is what you are looking for:

(x \ "endUsers")(0) \ "name"

But you dont need to write: val x:JsObject = obj(Map("endUsers" -> toJson(user) )), because user itself is List of JsObject. Hence user(0) \ "name" will do the same.

Details: https://www.playframework.com/documentation/2.3.x/ScalaJson