scala - JsPath.json.update doesn't work with array element in the path (IdxPathNode)? -



scala - JsPath.json.update doesn't work with array element in the path (IdxPathNode)? -

are eyes deceiving me or can not update nested node jspath containing array element in between? (e.g. /a/b(0)/c)

val pnjson = json.parse("""{"a": {"b": [ {"c": { "d": 1 } } ] } } """) val pnjspath = (__ \ "a" \ "b")(0) \ "c" val pntrans = pnjspath.json.update ( __.read[jsobject].map{ _ ++ json.obj( "e" -> 2 )} ) pnjson.transform(pntrans)

//result: java.lang.runtimeexception: expected keypathnode

if cant utilize __.json.update, how can accomplish this? trying accomplish immutablity.

the short reply you can't jspath. ticket mentions using jszipper library improve way manipulate json. if take utilize it, can do:

scala> import play.api.libs.json.extensions._ import play.api.libs.json.extensions._ scala> import play.api.libs.json.monad.syntax._ import play.api.libs.json.monad.syntax._ scala> val pnjson = json.parse("""{"a": {"b": [ {"c": { "d": 1 } } ] } } """) pnjson: play.api.libs.json.jsvalue = {"a":{"b":[{"c":{"d":1}}]}} scala> val pnjspath = (__ \ "a" \ "b")(0) \ "c" pnjspath: play.api.libs.json.jspath = /a/b(0)/c scala> pnjson.update(pnjspath, _.set(__ \ "e" -> jsnumber(2))) res0: play.api.libs.json.jsvalue = {"a":{"b":[{"c":{"d":1,"e":2}}]}}

otherwise, best bet break task 2 parts, first part beingness parsing array , sec part beingness transforming new array modified contents.

json scala playframework playframework-json

Comments

Popular posts from this blog

c - Compilation of a code: unkown type name string -

java - Bypassing "final local variable defined in an enclosing type" -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -