0

I am passing and payload which is similar as the case class reportdata, inside a class. And i need a to get the value of report_data which is a Option[JSArray],

I need assign that JSArray to a varible if that optional array matches the case class reportdata

case class Fields(
                      reportid: Option[Long],
                      report_data: Option[JsArray],
                      header :Option[JsArray],
                      footer :Option[JsArray]
                    )

case class reportdata(
                     customText : Option[String],
                     textAlignment: Option[String],
                     textSize : Option[Int],
                     pageHeight: Long,
                     pageWidth: Long
                     )

The Json i pass from the DB is case class type Fields, and it has 3 JSON Arrays. so i want select the json Array which matches the case class of report data and I should assign it to a new variable.

"reports": [
        {
            "reportid":513,
            "report_data":[
                {
                    "formatType": "text",
                    "value": "This is a sample text to be printed in the report"
                },
                {
                    "formatType": "text size",
                    "value": 12
                },
                {
                    "formatType": "text alignment",
                    "value" : "RIGHT"
                },
                {
                    "formatType": "page height",
                    "value" : "12"
                },
                {
                    "formatType": "page width",
                    "value" : "8"
                }
            ],
            "header": [
                {
                    "formatType": "text",
                    "value": "Test"
                },
                {
                    "formatType": "font size",
                    "value": 12
                }
            ],
            "footer": [
                {
                    "formatType": "text",
                    "value": "Test"
                },
                {
                    "formatType": "font size",
                    "value": 12
                }
            ]
        }
    ]
1
  • 1
    Please clarify your question. What have you tried / what library do you use. Commented Jan 9, 2020 at 6:56

1 Answer 1

1

Use Dijon FTW!

Here is a test that demonstrates how easily the "right" value can be found in samples like yours:

    import com.github.pathikrit.dijon._

    val json = parse(
      """{
        |"data":[
        |  {
        |    "formatType": "text",
        |    "value": "bgyufcie huis huids hufhsduhfsl hd"
        |  },
        |  {
        |    "formatType": "text size",
        |    "value": 12
        |  },
        |  {
        |    "formatType": "text alignment",
        |    "value" : "right"
        |  }
        |]
        |}""".stripMargin)

assert(json.data.toSeq.collect { 
  case obj if obj.formatType == "text alignment" => obj.value 
}.head == "right")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.