Flatten extranous fields when extracting using LiftJson or Json4s - scala

I want to extract using LiftJson or Json4s the following Json (not quite but something similar) to the following case classes.
{
"data": [
{
"id": "1234",
"message": "Test",
"comments": {
"data": [
{
"id": "4321",
"content": "Test2",
}
]
}
}
The case classes:
case class A(id: String, message: string, comments: List[B])
case class B(id: String, content: String)
For the top level I can do: (val \ "data").extract[List[A]] to flatten the extra data field. But for the second level one, I don't see I way to use extract DIRECTLY.
Could I use a custom serializer (exemple here) or any of the following function (json4s) to remove the extraneous "data" field? Or any idea to make it simple?
def mapField(f: JField => JField): JValue
def transformField(f: PartialFunction[JField, JField]): JValue
Want I want to avoid is creating others intermidiate case class to extract the data, and then create the shown case class with it.

I found the solution a while ago, but haven't had time to reply. I was thinking backward, this is easy:
def transformListData(src: JValue, field: String): JValue = {
src.transformField {
case JField(x, v) if x == field => JField(field, v \ "data")
}
}
transformListData(json, "comments")
The following would remove the extra data and would flatten the list.

Related

json to scala case class

I am trying to read the below json into scala case class. I am able to bind the case class to json using json4s.
The problem is the expectedTypes would change for every table. It could be more or less number of elements and the name would be different. How to create a case class for this requirement?
{
"filepattern": "product*.gzip",
"replaceheader": "productid,name,market",
"dataType": [
{
"expectedTypes": {
"productId": "DOUBLE",
"name": "STRING"
}
}
]
}
case class ExpectedTypes(
productid: String,
name: String
)
case class DataType(
expectedTypes: ExpectedTypes
)
case class table(
filepattern: String,
replaceheader: Option[String],
dataType: List[DataType]
)
If it's not predictable how many fields are you going to have in expectedTypes, you can use Map:
case class Root(
filepattern: String,
replaceheader: Option[String],
dataType: List[DataType]
)
case class DataType(
expectedTypes: Map[String, String]
)

Handling nulls with json Play

I am trying to parse json with null values for some fields using Play library. There is a case class which represents the data:
case class Id(value: Int) extends AnyVal
case class Name(value: String) extends AnyVal
case class Number(value: Int) extends AnyVal
case class Data(id: Option[Id], name: Option[Name], number: Option[Number])
Here is how parsing currently works:
def parse(jsValue: JsValue): Try[Seq[Data]] = Try {
jsValue.as[JsArray].value
.flatMap { record =>
val id = Id((record \ "id").as[Int])
val name = Name((record \ "name").as[String])
val number = Number((record \ "number").as[Int])
Some(Data(Some(id), Some(name), Some(number)))
}
}
Parsing with specific data types doesn't handle null cases, so this implementation returns:
Failure(play.api.libs.json.JsResultException: JsResultException(errors:List((,List(JsonValidationError(List(error.expected.jsstring),WrappedArray()))))))
For the input data like this:
{
"id": 1248,
"default": false,
"name": null,
"number": 2
}
I would like to have something like this: Seq(Data(Some(Id(1248)), None, Some(Number(2))))
I am going to write the data into the database so I do not mind writing some null values for these fields.
How can I handle null values for fields in parsed json?
You can simply let the play-json library generate the Reads for your case classes instead of writing them manually:
import play.api.libs.json._
object Data {
implicit val reads: Reads[Data] = {
// move these into the corresponding companion objects if used elsewhere...
implicit val idReads = Json.reads[Id]
implicit val numberReads = Json.reads[Number]
implicit val nameReads = Json.reads[Name]
Json.reads[Data]
}
}
def parse(jsValue: JsValue): Try[Seq[Data]] = Json.fromJson[Seq[Data]](jsValue).toTry
That way, your code will work even in case you change the arguments of your case classes.
If you still want to code it manually, you can use the readNullable parser:
val name: Option[Name] = Name(record.as((__ \ "name").readNullable[String]))
Note, however, that using Try is somewhat frowned upon in FP and directly using JsResult would be more idiomatic.
If you are not locked to use play-json then let me show how it can be done easily with jsoniter-scala:
import com.github.plokhotnyuk.jsoniter_scala.core._
import com.github.plokhotnyuk.jsoniter_scala.macros._
implicit val codec: JsonValueCodec[Seq[Data]] = JsonCodecMaker.make(CodecMakerConfig)
val json: Array[Byte] = """[
{
"id": 1248,
"default": false,
"name": null,
"number": 2
}
]""".getBytes("UTF-8")
val data: Seq[Data] = readFromArray(json)
println(data)
That will produce the following output:
List(Data(Some(Id(1248)),None,Some(Number(2))))
Here you can see an example how to integrate it with the Play framework.

Building Reads converter and case class at runtime in Play Framework

I have a file that contains the following array of JSON objects:
[
{
"type": "home",
"number": 1111
},
{
"type": "office",
"number": 2222
},
{
"type": "mobile",
"number": 3333
}
]
In Play Framework 2.x I would define an implicit Reads converter to read the file and convert it to a Scala structure:
implicit val implicitRead : Reads[MyClass] = (
(JsPath \ "type").read[String] and
(JsPath \ "number").read[Int]
) (MyClass.apply _)
the Scala case class defined as:
case class MyClass (myType: String, myNumber: Int)
and parsing the JSON with:
val json = // file record content
json.validate[MyClass] match {
case s: JsSuccess[MyClass] => {
val myObject: MyClass = s.get
// do something with myObject
}
case e: JsError => {
// error handling flow
}
Now, my problem is that I know the structure of the JSON file only at runtime, not at compilation time. Is it possible to build both the implicit Reads converter and the case class at runtime?
Use case classes directly with play-json:
Change the case class to:
case class MyClass (`type`: String, number: Int)
Add the json-formatter to the companion object:
object MyClass {
implicit val format = Json.format[MyClass]
}
The validate function looks now:
val myClass = // file record content
json.validate[Seq[MyClass]] match {
case JsSuccess(myClasses, _) => myClasses
case e: JsError => // handle error case
}
That's all you need. If you are not happy with the parameter names, you can use a Wrapper case class.

How to serialize a Scala object to Json which already contains some Json

I have the following object which I am serializing to json using Circe
case class Person(name: String, data: String)
val x = Person("test", s"""{"a": 10, "b":"foo"}""")
import io.circe._, io.circe.generic.auto._, io.circe.parser._, io.circe.syntax._
println(x.asJson)
The output of the statement above is
{
"name" : "test",
"data" : "{\"a\":10, \"b\":\"foo\"}"
}
but the output I want is
{
"name": "test",
"data": {
"a": 10,
"b": "foo"
}
}
I get the data for the data field from a json based data store. I want to pass it through (so I don't want to unmarshall it into a scala object, only to demarshall it again into json. that marshall/demarshall is a waste of CPU on my server.
So how do I handle such data?
Well, you can write your own Encoder implementation, e.g.:
import io.circe.{Encoder, Json}
import io.circe.jawn.parse
case class Person(name: String, data: String)
implicit val personEncoder: Encoder[Person] = new Encoder[Person] {
override def apply(person: Person): Json = {
val name = Json.fromString(person.name)
val data = parse(person.data) match {
case Left(_) => Json.obj()
case Right(value) => value
}
Json.obj("name" -> name, "data" -> data)
}
}
As a matter of the fact, you have pretty unusual case - one of the fields is a String, that you need to parse, before you put it as a child Json node. Error failure need to be handled somehow - I used empty object, but that isn't necessarily what you would like to use.
If you want to omit the deserialization step... that is not possible. You are building tree of JSON nodes with defined behavior. String cannot suddenly be treated like Json object.

How to feed JSON to CASE CLASS directly using functional programming in scala?

{
"cars": [{
"amount": 120.00,
"name": "Car1"
}, {
"amount": 245.00,
"name": "Car2"
}]
}
I am reading above JSON as following in my Controller
val body: JsObject = request.body.asInstanceOf[JsObject]
I am having following CASE CLASS
case class BIC(name: String, amount: Double)
I want to create List[BIC] objects by reading data from JSON [e.g. body] using Functional style
Use Play JSON.
Example:
case class Wrapper(cars: List[Bic])
case class BIC(name: String, amount: Double)
Then in your controller:
implicit val wrapperFormats = Json.format[Wrapper]
implicit val bICFormats = Json.format[BIC]
def postCars(): Action[JsValue] = Action(json.parse) { implicit request =>
request.body.validate[Wrapper] match {
case JsSuccess(obj, _) => {
//do something with obj.
}
case JsError(err) => {
BadRequest(
JsObject(
"error" -> err.toString
)
)
}
}
}
Please note that I am returning Action[JsValue] this is so JQuery will run success when using AJAX.
I hope this helps,
Rhys
another reference:
https://www.playframework.com/documentation/2.5.x/ScalaJsonCombinators
First, define two case classes for your model like this :
object Models {
case class Bic(name : String, amount : Double)
object Bic {
implicit val BicFormat = Json.format[Bic]
}
case class Cars(bics : List[Bic])
object Cars {
implicit val CarsFormat = Json.format[Cars]
}
}
You're using the Play Framework so you can use the JSON library.
In your controller, if you want to read the bics, you can do it like that :
def getCars = Action(parse.json) { request =>
request.body.validate[Cars] map { cars =>
// treat your cars ..
}
}