简体   繁体   中英

How to properly validate and transform a json in play scala?

I am trying to parse and transform a json file so that it can fit a DB structure. I have the following json file.

{
  "request": {
    "Target": "AR",
    "Format": "jsonp",
    "Service": "HasOff",
    "Version": "2",
    "NetworkId": "xyz",
    "Method": "getConversions",
    "api_key": "xyz",
    "fields": [
      "Offer.name",
      "Stat.datetime",
      "Stat.conversion_status",
      "Stat.approved_payout",
      "Stat.ip",
      "Stat.ad_id",
      "Stat.affiliate_info1",
      "Stat.offer_id"
    ],
    "data_start": "2015-04-16 23:59:59",
    "data_end": "2015-04-18 00:00:00",
    "callback": "angular.callbacks._3",
    "_ga": "GA1.2.1142892596.1429262595",
    "_gat": "1"
  },
  "response": {
    "status": 1,
    "httpStatus": 200,
    "data": {
      "page": 1,
      "current": 100,
      "count": 521,
      "pageCount": 6,
      "data": [
        {
          "Offer": {
            "name": "ABC Company"
          },
          "Stat": {
            "datetime": "2015-04-19 12:09:01",
            "conversion_status": "approved",
            "approved_payout": "26.94000",
            "ip": "111.11.11.1",
            "ad_id": "123456",
            "affiliate_info1": "123456",
            "offer_id": "260"
          }
        },
        {
          "Offer": {
            "name": "ABC Company"
          },
          "Stat": {
            "datetime": "2015-04-11 01:01:38",
            "conversion_status": "approved",
            "approved_payout": "44.94000",
            "ip": "49.204.222.117",
            "ad_id": "123456",
            "affiliate_info1": "123456",
            "offer_id": "260"
          }
        },

To process it I wrote a case class, model the class and try to convert the json into the model as given in -- https://www.playframework.com/documentation/2.2.x/ScalaJson . The code snippet is given below. I have reduced the number of parameters in the case class for sake of brevity.

case class ConversionReport(offerName: String, date: DateTime)
implicit val readConversion: Reads[ConversionReport] = (
      (__ \ "response" \ "data" \ "data" \ "Offer" \ "name").read[String] and
      (__ \ "response" \ "data" \ "data" \ "Stat" \ "datetime").read[DateTime]
  ) (ConversionReport)

  def getConversionReport(){

    val conversionUrl: String = "http://some_link" 
    val holder = WS.url(conversionUrl).get()
    val result = Await.result(holder, Duration(10, "second")).json
    val conversionResult: JsResult[ConversionReport] = result.validate[ConversionReport]
    println ("xxxx The conversion result is " +conversionResult)

  }

When I execute the code, I am getting the error --

xxxx The conversion result is JsError(List((/offerName,List(ValidationError(error.path.missing,WrappedArray()))), (/date,List(ValidationError(error.path.missing,WrappedArray())))))

I guess, I am not able to properly model the json file. Any help is appreciated.

In this case data is an array, so your approach will not work. You will have to change few things.

import play.api.libs.json._
import play.api.libs.json.Reads._

case class ConversionReport(offerName: String, date: DateTime)

implicit val readConversion: Reads[ConversionReport] = (
  (__\ "Offer" \ "name").read[String] and
  (__ \ "Stat" \ "datetime").read[DateTime]
) (ConversionReport)

case class FullReport( reportList: Seq[ ConversionReport ] )
implicit val readFullReport: Reads[ FullReport ] = (
  (__ \ "response" \ "data" \ "data").lazyRead( Reads.seq[ ConversionReport ]( readConversion ) )
) ( FullReport )

def getConversionReport(){

  val conversionUrl: String = "http://some_link" 
  val holder = WS.url(conversionUrl).get()
  val result = Await.result(holder, Duration(10, "second")).json

  val conversionResult: JsResult[FullReport] = result.validate[FullReport]
  println ("xxxx The conversion result is " +conversionResult)

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM