Home > Back-end >  Scala Json parsing
Scala Json parsing

Time:04-05

This is the input json which I am getting which is nested json structure and I don't want to map directly to class, need custom parsing of some the objects as I have made the case classes

{
    "uuid": "b547e13e-b32d-11ec-b909-0242ac120002",
    "log": {
        "Response": {
            "info": {
                "receivedTime": "2022-02-09T00:30:00Z",
                "isSecure": "Yes",

                "Data": [{
                    "id": "75641",
                    "type": "vendor",
                    "sourceId": "3",
                    "size": 53
                }],
                "Group": [{
                        "isActive": "yes",
                        "metadata": {
                            "owner": "owner1",
                            "compressionType": "gz",
                            "comments": "someComment",
                            "createdDate": "2022-01-11T11:00:00Z",
                            "updatedDate": "2022-01-12T14:17:55Z"
                        },
                        "setId": "1"
                    },
                    {
                        "isActive": "yes",
                        "metadata": {
                            "owner": "owner12",
                            "compressionType": "snappy",
                            "comments": "someComment",
                            "createdDate": "2022-01-11T11:00:00Z",
                            "updatedDate": "2022-01-12T14:17:55Z"
                        },
                        "setId": "2"
                    },
                    {
                        "isActive": "yes",

                        "metadata": {
                            "owner": "owner123",
                            "compressionType": "snappy",
                            "comments": "someComment",
                            "createdDate": "2022-01-11T11:00:00Z",
                            "updatedDate": "2022-01-12T14:17:55Z"
                        },
                        "setId": "4"
                    },
                    {
                        "isActive": "yes",
                        "metadata": {
                            "owner": "owner124",
                            "compressionType": "snappy",
                            "comments": "someComments",
                            "createdDate": "2022-01-11T11:00:00Z",
                            "updatedDate": "2022-01-12T14:17:55Z"
                        },
                        "setId": "4"
                    }
                ]
            }
        }
    }
}

Code that I am trying play json also tried circe . Please help ..New to scala world

below is object and case class

case class DataCatalog(uuid: String, data: Seq[Data], metadata: Seq[Metadata])

object DataCatalog {
case class Data(id: String,
                       type: String,
                       sourceId: Option[Int],
                       size: Int)

case class Metadata(isActive: String,
                    owner: String,
                    compressionType: String,
                    comments: String,
                    createdDate: String,
                    updatedDate: String
                    )


 def convertJson(inputjsonLine: String): Option[DataCatalog] = {
    val result = Try {
      //val doc: Json = parse(line).getOrElse(Json.Null)
      //val cursor: HCursor = doc.hcursor
      //val uuid: Decoder.Result[String] = cursor.downField("uuid").as[String]
      
      val lat = (inputjsonLine \ "uuid").get

      
     
      DataCatalog(uuid, data, group)
    }
    //do pattern matching
    result match {
      case Success(dataCatalog) => Some(dataCatalog)
      case Failure(exception) =>
        
    }
  }
  }

Any parsing api is fine.

CodePudding user response:

If you use Scala Play, for each case class you should have an companion object which will help you a lot with read/write object in/from json:

object Data {
  import play.api.libs.json._

  implicit val read = Json.reads[Data ]
  implicit val write = Json.writes[Data ]

  def tupled = (Data.apply _).tupled
}

object Metadata {
  import play.api.libs.json._

  implicit val read = Json.reads[Metadata ]
  implicit val write = Json.writes[Metadata ]

  def tupled = (Metadata.apply _).tupled
}

Is required as each companion object to be in same file as the case class. For your json example, you need more case classes because you have a lot of nested objects there (log, Response, info, each of it)

or, you can read the field which you're interested in as:

(jsonData \ "fieldName").as[CaseClassName]

You can try to access the Data value as:

(jsonData \ "log" \ "Response" \ "info" \ "Data").as[Data]

same for Metadata

  • Related