play-json

play json in scala: deserializing json with unknown fields without losing them

ⅰ亾dé卋堺 提交于 2021-02-11 08:35:33
问题 consider i have a json as following: { "a": "aa", "b": "bb", "c": "cc", "d": "dd", // unknown in advance "e": { //unknown in advance "aa": "aa" } } i know for sure that the json will contain a,b,c but i've no idea what other fields this json may contain. i want to serialize this JSON into a case class containing a,b,c but on the other hand not to lose the other fields (save them in a map so the class will be deserialized to the same json as received). ideas? 回答1: One option is to capture the

How to update a nested json using scala play framework?

ぃ、小莉子 提交于 2021-02-05 07:28:29
问题 I am trying to update a json value present within a json using Scala play framework.Instead of updating the value it is appending the value. val newJsonString = """{"P123": 25}""" val jsonStringAsJsValue = Json.parse("""{"counter_holders": {"Peter": 25}}""") //jsonStringAsJsValue: play.api.libs.json.JsValue = {"counter_holders":{"Peter":25}} val jsonTransformer = (__ \"counter_holders" ).json.update(__.read[JsValue].map{o => Json.parse(newJsonString)}) jsonStringAsJsValue.transform

How to update a nested json using scala play framework?

你离开我真会死。 提交于 2021-02-05 07:27:08
问题 I am trying to update a json value present within a json using Scala play framework.Instead of updating the value it is appending the value. val newJsonString = """{"P123": 25}""" val jsonStringAsJsValue = Json.parse("""{"counter_holders": {"Peter": 25}}""") //jsonStringAsJsValue: play.api.libs.json.JsValue = {"counter_holders":{"Peter":25}} val jsonTransformer = (__ \"counter_holders" ).json.update(__.read[JsValue].map{o => Json.parse(newJsonString)}) jsonStringAsJsValue.transform

Play Framework 2.8.2 - No Json serializer found for type (subclass)

老子叫甜甜 提交于 2021-01-07 05:58:26
问题 I'm migrating some of my services from Play 2.7.x to the newest 2.8.2, together with scala 2.13.2 and sbt 1.3.12. I'm hitting an obstacle with the play-json though, and the Reads[A] I have the following setup: sealed trait Charge {} case class ChargeOne(one: Int) extends Charge case class ChargeTwo(two: Int) extends Charge object Charge { implicit val writes: Writes[Charge] = (charge: Charge) => {...} } We have some tests looking like so val chargeOne = ChargeOne(1) val json = Json.toJson

Play: How to remove the fields without value from JSON and create a new JSON with them

僤鯓⒐⒋嵵緔 提交于 2021-01-02 06:37:08
问题 Given the following JSON: { "field1": "value1", "field2": "", "field3": "value3", "field4": "" } How do I get two distinct JSONs, one containing the fields with value and another one containing the fields without value? Here below is how the final result should look like: { "field1": "value1", "field3": "value3" } { "field2": "", "field4": "" } 回答1: You have access to the JSON object's fields as a sequence of (String, JsValue) pairs and you can filter through them. You can filter out the ones

Play-json: Error on updating an object attribute inside an array

荒凉一梦 提交于 2020-02-25 08:41:31
问题 I am playing with play-json library to manipulate my json files. I have followed this official document and everything is working fine but they haven't told the case when we want to update a value of an object inside an array so I am trying this my own example. i.e. [ { "key1": [ { "key12": [ { "key121": "text1", "key122": 121212, "key123": "text2", "key124": [] } ], "key13": 0 } ], "key2": "value2" } ] In above example I am trying to update value of "key123" but getting this error again and

Play-json: Error on updating an object attribute inside an array

£可爱£侵袭症+ 提交于 2020-02-25 08:41:04
问题 I am playing with play-json library to manipulate my json files. I have followed this official document and everything is working fine but they haven't told the case when we want to update a value of an object inside an array so I am trying this my own example. i.e. [ { "key1": [ { "key12": [ { "key121": "text1", "key122": 121212, "key123": "text2", "key124": [] } ], "key13": 0 } ], "key2": "value2" } ] In above example I am trying to update value of "key123" but getting this error again and

Operation on List after flatten

大城市里の小女人 提交于 2020-02-06 07:42:13
问题 I perform a flatten operation on a Seq[JsValue] like: var rows = (response \\ "rows").head.as[List[List[String]]].flatten to get the following output: List(AA , BB , CC ,...) Basically the response I receive from JSON is that way, so extra spaces will be present to the end of all values. What I actually desire for is: List(AA, BB, CC, ...) Is it possible to perform any operation before/instead of flatten to get what I want to achieve? I tried a flatMap and the results were not too desirable.

Operation on List after flatten

ぐ巨炮叔叔 提交于 2020-02-06 07:42:05
问题 I perform a flatten operation on a Seq[JsValue] like: var rows = (response \\ "rows").head.as[List[List[String]]].flatten to get the following output: List(AA , BB , CC ,...) Basically the response I receive from JSON is that way, so extra spaces will be present to the end of all values. What I actually desire for is: List(AA, BB, CC, ...) Is it possible to perform any operation before/instead of flatten to get what I want to achieve? I tried a flatMap and the results were not too desirable.

No Json formatter for Option[String]?

只谈情不闲聊 提交于 2019-12-30 03:12:06
问题 I am trying to marshall and un-marshall an Option[String] field to and from JSON. For my use-case, a None value should be marshaled as "null". Here is the code I have: import org.scalatest.{FlatSpec, Matchers} import play.api.libs.json._ import play.api.libs.json.Reads._ import play.api.libs.functional.syntax._ case class Person( id: Int, firstName: Option[String], lastName: Option[String] ) object Person { implicit lazy val personFormat = ( (__ \ "id").format[Int] and (__ \ "first_name")