How to merge a JsValue to JsObject in flat level - json

I have two JsValue created from case class, i.e. Book and Book detail
val bookJson = Json.tojson(Book)
val bookDetailJson = Json.tojson(BookDetail)
and the format would be:
//Book
{
id: 1,
name: "A Brief History of Time"
}
//BookDetail
{
bookId: 1,
author: "Steven Hawking",
publicationDate: 1988,
pages: 256
}
How can I merge them to a single Json in play-framework 2.10? i.e.
//Book with detail
{
id: 1,
name: "A Brief History of Time",
bookId: 1,
author: "Steven Hawking",
publicationDate: 1988,
pages: 256
}
I was trying the transformation and failed to iterate through the second JsValue:
val mapDetail = (__).json.update(
__.read[JsObject].map { o =>
o.deepMerge( JsObject(Seq(("detail", bookDetailJson))) )
})
bookJson.validate(mapDetail).get
It would become one level down, which I don't really want.
//Book with detail
{
id: 1,
name: "A Brief History of Time",
detail: {
bookId: 1,
author: "Steven Hawking",
publicationDate: 1988,
pages: 256
}
}
Please let me know if any trick could provide on this Json transform. Many Thanks!

Play has a lot of new features for JSON right now. This would be a nice showcase for the Format[A] trait (see Scala Json Inception) which you could include implicitly as I will show, or explicitly to the methods that require an implicit Format[A]/Reads[A]/Writes[A].
Create a case class to represent your JSON objects,
case class Book(id: Int, name: String)
case class BookDetail(id: Int, author: String, publicationDate: Int, pages: Int)
Create companion objects that contain the implicit Format[A] so that Format/Reads/Writes will automatically be in scope when you need them.
object Book {
implicit val fmt: Format[Book] = Json.format[Book]
}
object BookDetail {
implicit val fmt: Format[BookDetail] = Json.format[BookDetail]
}
Now you could do something like this,
val bookJson = Json.toJson(Book(1, "A Brief History Of Time"))
val bookDetailJson = Json.toJson(BookDetail(1, "Steven Hawking", 1988, 256))
bookJson.as[JsObject].deepMerge(bookDetailJson.as[JsObject])
And you will have an object like this,
{
id: 1,
name: "A Brief History Of Time",
author: "Steven Hawking",
publicationDate: 1988,
pages: 256
}
I've tried this in the REPL but it does not work, in a Play application it does just fine though. Also in a production scenario we would likely use asOpt[T] in place of as[T].
Here is an example of why asOpt[T] may be better suited, suppose instead of a valid JSON object for book you get,
val bookJson = Json.toJson("not a book")
You will end up with a
[JsResultException: JsResultException(errors:List((,List(ValidationError(validate.error.expected.jsobject,WrappedArray())))))]
But suppose instead you change your method to use asOpt[T],
bookJson.asOpt[JsObject].getOrElse(Json.obj()).deepMerge(bookDetailJson.asOpt[JsObject].getOrElse(Json.obj()))
Now you will end up with at least a partial JSON object,
{
id: 1,
author: "Steven Hawking",
publicationDate: 1988,
pages: 256
}
So depending on how you would like to handle improperly formatted JSON you could choose either option.

JsObject is subtype of JsValue.
JsValue can be simple converted to the JsObject using as or asOpt methods from JsValue.
Example:
val someJsValue = ....
val asObject:JsObject = someJsValue.as[JsObject]
val asObjectMaybe:Option[JsObject] = v.asOpt[JsObject]
In the case of JsArray you can not use above code.
If you use play and parse JSON with array, then Json.toJson(...) produces JsValue which is JsArray actually.
You need to convert JsArray as following:
val someJsValueButArray = ....
val asJsArray:JsArray = Json.toJson(someJsValueButArray).as[JsArray]
val asSeqOfJsObjects:Seq[JsObject] = asJsArray.value.map(_.as[JsObject])

Related

Trying unparse json string, but getting Expected start of the object '{', but had 'EOF' instead

I am trying to parse a json file into a list using kotlin serializable.
Here are my data classes.
#Serializable
data class Book(
val epub : String,
val fb2 : String,
val mobi : String,
val djvu : String,
val title : String,
val author : String,
val anotation: String,
val cover_uri : String,
)
#Serializable
data class Books(
#Serializable (with = BookListSerializer::class)
val books : List<Book>
)
object BookListSerializer : JsonTransformingSerializer < List < Book >> ( ListSerializer ( Book.serializer ()))
Here I am trying to parse a string
val books = Json.decodeFromString<Books>(stringJson)
Here my Json String
[
{
"anotation": "Этот город",
"author": "Чарльз Плэтт",
"cover_uri": "null",
"djvu": "null",
"epub": "/b/301494/epub",
"fb2": "/b/301494/fb2",
"mobi": "/b/301494/mobi",
"title": "New York Times (Пульс Нью-Йорка) (fb2)"
},
{
"anotation": "Способна л",
"author": "Триш Уайли",
"cover_uri": "/i/45/390445/cover.jpg",
"djvu": "null",
"epub": "/b/390445/epub",
"fb2": "/b/390445/fb2",
"mobi": "/b/390445/mobi",
"title": "Лучший мужчина Нью-Йорка (fb2)"
}
]
And i always getting this error
kotlinx.serialization.json.internal.JsonDecodingException: Expected start of the object '{', but had 'EOF' instead
JSON input: .....2","mobi":"/b/49442/mobi","title":"I love New York (fb2)"}]
I would be very glad and grateful for any help
tl;dr
Exchange this
val books = Json.decodeFromString<Books>(stringJson)
with this
val books = Json.decodeFromString<List<Book>>(stringJson)
You're trying to deserialize an JSON array [ ... ] but declare an object of type Books as target when calling decodeFromString, thus something like { books: [ ... ] }.
You either have to wrap your JSON array in the property books of an JSON object or change the expected type during deserialization to List<Book>.
Thus, besides the above solution, you could also do the following:
val wrappedStringJson = """
{
"books": $stringJson
}
""".trimIndent()
val books = Json.decodeFromString<Books>(wrappedStringJson)
I experienced the same issue during testing on Ktor Server.
fun testFun() = testApplication { ....
val response = client.get("/boruto/heroes")
val actual = Json.decodeFromString<ApiResponse>(response.content.toString())
....
}
The issue was that I was using this content instead of body.
val actual = Json.decodeFromString<T>(response.content.toString())
I changed it to this and the test passed
val actual = Json.decodeFromString<T>(response.body())
Leaving this here in case someone encounters the same issue.

Get only one parameter from json object via retrofit(gson)

Json:
I need to get only id field from source. Data class:
data class Article(
val sourceId: String,
val author: String,
val title: String,
...
)
Convertor factory is GsonConvertorFactory
In the JSON you provided, source is a complex object, so you can't define it as a string unless you create a custom deserialiser. A quick way to get this working, though, is to create another set of classes to mimic the JSON structure, like this:
data class Source(
val id: String
)
data class Article(
val source: Source,
val author: String,
val title: String
)
Then you can use it this way:
fun main() {
val json = """ {
"source": {
"id": "bbc-news",
"name": "BBC News"
},
"author": "BBC News",
"title": "Afrobeat pioneer Tony Allen dies aged 79"
}
""".trimIndent()
val gson = GsonBuilder().create()
val article = gson.fromJson(json, Article::class.java)
println(article)
}
This prints: Article(source=Source(id=bbc-news), author=BBC News, title=Afrobeat pioneer Tony Allen dies aged 79).

json4s parse json partially

I have a json model, where contents of certain attribute depend on the other attribute. Something like this:
"paymentMethod": "CREDIT_CARD",
"metaData": {
"cardType": "VISA",
"panPrefix": "",
"panSuffix": "",
"cardHolder": "",
"expiryDate": ""
}
So when paymentMethod equals to CREDIT_CARD, the metadata object will contain attributes as described. In case of other payment method, there'll be different metadata.
I want to handle this situation in a future-proof way. What I'm trying to do is to not parse the metadata field right away, but keep it somehow "unparsed" until I've parsed the paymentMethod field. Then I'd take the metadata and applied appropriate parsing approach.
However I don't know which type to use for a Scala class field for such "late parsed" attributes. I've tried String, JsonInput, JObject, and they all are not suitable (either don't compile or can't be parsed). Any ideas which type can I use? Or, in other words:
case class CreditCardMetadata(
cardType: String,
panPrefix: String,
panSuffix: String,
cardHolder: String,
expiryDate: String)
case class PaypalMetadata(...) // etc.
case class PaymentGatewayResponse(
paymentMethod: String,
metadata: ???)
You could create a CustomSerializer to parse the metadata directly. Something like :
case class PaymentResponse(payment: Payment, otherField: String)
sealed trait Payment
case class CreditCardPayment(cardType: String, expiryDate: String) extends Payment
case class PayPalPayment(email: String) extends Payment
object PaymentResponseSerializer extends CustomSerializer[PaymentResponse]( format => (
{
case JObject(List(
JField("paymentMethod", JString(method)),
JField("metaData", metadata),
JField("otherField", JString(otherField))
)) =>
implicit val formats = DefaultFormats
val payment = method match {
case "CREDIT_CARD" => metadata.extract[CreditCardPayment]
case "PAYPAL" => metadata.extract[PayPalPayment]
}
PaymentResponse(payment, otherField)
},
{ case _ => throw new UnsupportedOperationException } // no serialization to json
))
Which can be used as:
implicit val formats = DefaultFormats + PaymentResponseSerializer
val json = parse("""
{
"paymentMethod": "CREDIT_CARD",
"metaData": {
"cardType": "VISA",
"expiryDate": "2015"
},
"otherField": "hello"
}
""")
val json2 = parse("""
{
"paymentMethod": "PAYPAL",
"metaData": {
"email": "foo#bar.com"
},
"otherField": "world"
}
""")
val cc = json.extract[PaymentResponse]
// PaymentResponse(CreditCardPayment(VISA,2015),hello)
val pp = json2.extract[PaymentResponse]
// PaymentResponse(PayPalPayment(foo#bar.com),world)
You can use a Map[String, String].
It will contain anything you may need.
The answer by Peter Neyens has inspired me to implement my own solution. It's not as generic as his, but in my case I needed something really simple and ad-hoc. Here's what I've done:
It's possible to define a case class with the field of unknown type is represented by a JObject type. Something like this:
case class PaymentGatewayResponse(
default: Boolean,
paymentMethod: String,
visibleForCustomer: Boolean,
active: Boolean,
metaData: JObject)
When such json is parsed into such case class, this field is not parsed immediately, but contains all the necessary information. Then it's possible parse it in a separate step:
case class CreditCardMetadata(
cardType: String,
cardObfuscatedNumber: String,
cardHolder: String,
expiryDate: String)
val response: PaymentGatewayResponse = doRequest(...)
response.map { r =>
r.paymentMethod match {
case "CREDIT_CARD" => r.metaData.extract[CreditCardMetadata]
case unsupportedType: String => throw new UnsupportedPaymentMethodException(unsupportedType)
}
}

Read JSON Tree structure in Scala Play Framework

I am trying to process an Ajax POST request in Play Framework 2.1.3. The post data is a JSON object and has a tree structure like:
{ id: "a", name: "myname", kids : [{ id: "a1", name : "kid1", kids: []}, {id: "a2", name: "kid2", kids: [{id: "aa1", name :"grandkid", kids: []}]}]
I would like to nest the 'children' arbitrarily deep. The class I would have in mind is like this (I realize the recursiveness can be problematic):
case class Person {
id: String,
name: String,
kids: Array[Person]
}
The format I would have in mind:
implicit val personFormat:Format[Person] = Json.format[Person]
Play is throwing errors on my Format that I wrote:
type mismatch; found : controllers.Resources.Person required: Array[controllers.Resources.Person]
I am aware that Play has a Tree structure. I couldn't find examples/documentation on how to tie that to JSON reads.
Any help is highly appreciated, thanks
You will need a recursive val, something like:
implicit val jsonReads: Reads[Person] = ((__ \ "id").read[String] and (__ \ "name").read[String] and (__ \ "kids").read[Seq[Person]])(apply _)
(I've changed the collection type from Array to Seq because it's more general and allows you to change your implementation without affecting downline code.)
This is using the syntax documented here.
The only way that I see this working is using either JsArray or Array[String] instead of Array[Person] in your Person case class. The JSON Macro Inception can only generate reads + writes code for primitives and lists, maps, and arrays for objects for which there already exist implicit JSON read + write code. Essentially you can't have a case class that references itself.
package models
import play.api.libs.json._
case class Person(
id : String,
name : String,
kids : JsArray
)
object Person extends ((String,String,JsArray) => Person) {
implicit val jsonFormat = Json.format[Person]
}

Extracting lift-json into a case class with an upper bound

I have spent the last day searching and reading various sites and articles to try to find the answer to this question myself and I haven't found anything that helps. I'm not even sure if this is feasible. My problem is that I am trying to parse and extract a Json Response using lift-json. The response consists of 4 parts, where the first 3 parts are always the same for every response to every type of request I make. The last part varies depending on the type of request, but it is always going to be a list of some type. I was hoping to do something like this:
abstract class MyObjects
case class Apple(id: Int, name: String, color: String) extends MyObjects
case class Orange(id: Long, name: String, state: String) extends MyObjects
abstract class MyResponse
case class Fruits[T <: MyObjects](aisle: Int, bin: Int, hasWhat: Option[List[T]])
Where if I wanted to know what all the apples are, I would make a request for that and get back a response with a list of apples. When I try to extract this example:
myJson.extract[Fruits[Apple]]
I get this error:
net.liftweb.json.MappingException: do not know how to get type parameter from T
at net.liftweb.json.Meta$.fail(Meta.scala:128)
at net.liftweb.json.Meta$Reflection$.term$1(Meta.scala:206)
at net.liftweb.json.Meta$Reflection$.typeParameters(Meta.scala:220)
at net.liftweb.json.Meta$.mkContainer$1(Meta.scala:91)
at net.liftweb.json.Meta$.fieldMapping$1(Meta.scala:101)
at net.liftweb.json.Meta$.mkContainer$1(Meta.scala:90)
at net.liftweb.json.Meta$.fieldMapping$1(Meta.scala:107)
at net.liftweb.json.Meta$.toArg$1(Meta.scala:117)
at net.liftweb.json.Meta$$anonfun$constructors$1$1$$anonfun$apply$1.apply(Meta.scala:83)
at net.liftweb.json.Meta$$anonfun$constructors$1$1$$anonfun$apply$1.apply(Meta.scala:82)
at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:...
I'm using lift-json 2.1 & scala 2.8. I do have a way to work around it, by specifically creating a case class for each type of response, but I thought what I was trying to do was cleaner. Just wanted to know if
a) is this even possible?
b) if so, what am I doing wrong?
EDIT...sample application:
val apples = """{ "aisle" : 1, "bin" : 1,
"hasWhat" : [{ "id" : 4, "name" : "Granny", "color" : "green"},
{ "id" : 4, "name" : "Fuji", "color" : "red"}] }"""
val oranges = """ { "aisle" : 3, "bin" : 2,
"hasWhat" : [{ "id" : 2, "name" : "Navel", "state" : "FL" },
{ "id" : 2, "name" : "Clementine", "state" : "Spain" }]}"""
scala> val aJson = parse(apples)
aJson: net.liftweb.json.JsonAST.JValue = JObject(List(JField(aisle,JInt(1)), JField(bin,JInt(1)), JField(hasWhat,JArray(List(JObject(List(JField(id,JInt(4)), JField(name,JString(Granny)), JField(color,JString(green)))), JObject(List(JField(id,JInt(4)), JField(name,JString(Fuji)), JField(color,JString(red)))))))))
scala> val oJson = parse(oranges)
oJson: net.liftweb.json.JsonAST.JValue = JObject(List(JField(aisle,JInt(3)), JField(bin,JInt(2)), JField(hasWhat,JArray(List(JObject(List(JField(id,JInt(2)), JField(name,JString(Navel)), JField(state,JString(FL)))))))))
scala> val doesntWork = aJson.extract[Fruits]
doesntWork: org.spin.node.gogrid.objects.Fruits = Fruits(1,1,None)
scala> val works = aJson.extract[AFruit]
works: org.spin.node.gogrid.objects.AFruit = AFruit(1,1,Some(List(Apple(4,Granny,green), Apple(4,Fuji,red))))
I want doesntWork to be like works, Where:
case class AFruit(aisle: Int, bin: Int, hasWhat: Option[List[Apple]])
Thanks!
-newbie
Extracting parameterized case class is not yet supported. One workaround (not sure if this works for your case though) is to make Fruits a concrete type and add the type information into JSON.
import net.liftweb.json._
import net.liftweb.json.Extraction._
import net.liftweb.json.JsonAST._
import net.liftweb.json.Printer._
abstract class MyObjects
case class Apple(id: Int, name: String, color: String) extends MyObjects
case class Orange(id: Long, name: String, state: String) extends MyObjects
case class Fruits(aisle: Int, bin: Int, hasWhat: Option[List[MyObjects]])
object Test extends Application {
// This configuration adds an extra field for MyObjects to JSON
// which tells the real type of a MyObject.
implicit val formats = Serialization.formats(FullTypeHints(List(classOf[MyObjects])))
val fs = Fruits(0, 0, Some(List(
Apple(1, "Granny Smith", "green"),
Apple(2, "Grenade", "red"))))
val json = decompose(fs)
println(pretty(render(json)))
assert (json.extract[Fruits] == fs)
}
That prints:
{
"aisle":0,
"bin":0,
"hasWhat":[{
"jsonClass":"Apple",
"id":1,
"name":"Granny Smith",
"color":"green"
},{
"jsonClass":"Apple",
"id":2,
"name":"Grenade",
"color":"red"
}]
}