playframework - how to update json's indexed array item
json, playframework
Solution
You need to select the whole array and transform that into a new array like this:
val transform= (__ \ "key1" ).json.update(
__.read[JsArray].map { a =>
JsArray(a.value.updated(0, a(0).as[JsObject] ++ Json.obj("key111" -> "value111")))
}
)
Problem
I am having a problem with adding an json object into an array item. This problem has been troubling me for quite some time but I still have no clue how to fix it. I want to add a json object in an array item, below is the existing json object: ``` { "key1":[ {"key11":"value11"}, {"key12":"value12"} ] } ``` I want it be transformed with a new json object added : ``` { "key1":[ {"key11":"value11" , "key111":"value111"}, {"key12":"value12"} ] } ``` Here is my code : ``` val json = Json.obj( "key1" ->Json.arr( Json.obj("key11" -> "value11"), Json.obj("key12" -> "value12") ) ) val transform= (__ \ 'key1 )(0).json.update( __.read[JsObject].map{ o => o ++ Json.obj( "key111" -> "value111" ) } ) json.validate(transform) ``` But the last line gives below exceptions. Is there anyone can give me suggestions on how to achieve my goal of adding an json object into an array item ? ``` java.lang.RuntimeException: expected KeyPathNode at play.api.libs.json.JsPath$.step$1(JsPath.scala:141) at play.api.libs.json.JsPath$.step$1(JsPath.scala:144) at play.api.libs.json.JsPath$.play$api$libs$json$JsPath$$buildSubPath$1(JsPath.scala:150) at play.api.libs.json.JsPath$$anonfun$createObj$1.apply(JsPath.scala:155) at play.api.libs.json.JsPath$$anonfun$createObj$1.apply(JsPath.scala:153) at scala.collection.IndexedSeqOptimized$class.foldl(IndexedSeqOptimized.scala:51) at scala.collection.IndexedSeqOptimized$class.foldLeft(IndexedSeqOptimized.scala:60) at scala.collection.mutable.WrappedArray.foldLeft(WrappedArray.scala:34) at play.api.libs.json.JsPath$.createObj(JsPath.scala:153) at play.api.libs.json.PathReads$$anonfun$jsUpdate$1$$anonfun$apply$15.apply(JsConstraints.scala:81) at play.api.libs.json.PathReads$$anonfun$jsUpdate$1$$anonfun$apply$15.apply(JsConstraints.scala:81) at play.api.libs.json.JsResult$class.map(JsResult.scala:73) ``` Thanks for listening Cyril