Search code examples
scalaapache-sparkamazon-s3playframeworkplay-json

Parse Json file on S3 using Json Play using Scala


I want to access a json file from S3 using json play fromework

val creds:DefaultAWSCredentialsProviderChain = new DefaultAWSCredentialsProviderChain
val s3Client = new AmazonS3Client(creds)
val uri: AmazonS3URI = new AmazonS3URI(conf_file)
val s3Object: S3Object = s3Client.getObject(uri.getBucket, uri.getKey)

val json = Json.parse(s3Object.getObjectContent)

val mylist = (json \ "mydata").get.as[List[JsValue]]

But this line gives an error

val mylist = (json \ "mydata").get.as[List[JsValue]]

as

no such element "mydata"

Can anyone tell how to access a json file and read its contents using json play in scala.

I am able to access same file from local machine, as well as fetch contents of "mydata" from within json


Solution

  • Did you tried to Print first the object and check if it is formatted properly to JSON, because everything seems works fine to me.

    val json: JsValue = Json.parse("""{
      "mydata": [
        {"first": "aa"},
        {"second": "bb"},
        {"third": "cc"}
      ]
    }""")
    

    Try to implement something like this.

    (json \ "mydata").asOpt[Seq[JsValue]].getOrElse(None)