score:1

Accepted answer

As you noticed if you use PlayJSON you can use Json Transformers

Updating a field would work like this:

val queryUpdater = (__ \ "definitions" \ 1 \ "SearchQuery" \ "query").json.update(
  of[JsString].map {
    case JsString(value) =>
      val newValue: String = ... // calculate new value
      JsString(newValue)
  }
)

json.transform(queryUpdater)

If you needed to update all queries it would be more like:

val updateQuery = (__ \ "SearchQuery" \ "query").json.update(
  of[JsString].map {
    case JsString(value) =>
      val newValue: String = ... // calculate new value
      JsString(newValue)
  }
)

val updateQueries = (__ \ "definitions").json.update(
  of[JsArray].map {
    case JsArray(arr) =>
      JsArray(arr.map(_.transform(updateQuery)))
  }
)

json.transform(updateQueries)

Related Query

More Query from same tag