0

I have the JSon response as given below. If metadata's Organic=true then label='true-Organic', else label='non-Organic' in the end => return List or Map[modelId,label]

import net.liftweb.json.{DefaultFormats, _}


object test1 extends App {

  val json_response =
    """{
  "requestId": "91ee60d5f1b45e#316",
  "error": null,
  "errorMessages": [
  ],
  "entries": [

  {

    "modelId":"RT001",
    "sku": "SKU-ASC001",
    "store": "New Jersey",
    "ttlInSeconds": 8000,
    "metadata": {
      "manufactured_date": "2019-01-22T01:25Z",
      "organic": "true"
    }
  },
  {

    "modelId":"RT002",
    "sku": "SKU-ASC002",
    "store": "livingstone",
    "ttlInSeconds": 8000,
    "metadata": {
      "manufactured_date": "2019-10-03T01:25Z",
      "organic": "false"
    }
  }

  ] }"""

tried like this :

 val json = parse(json_response)
  implicit val formats = DefaultFormats
  var map = Map[String, String]()

  case class Sales(modelId: String, sku: String, store: String, ttlInSeconds: Int, metadata:
  Map[String, String])

  case class Response(entries: List[Sales])

  val response = json.extract[Response]

After this, not sure how to proceed.

Seth Tisue
  • 29,985
  • 11
  • 82
  • 149
byomjan
  • 119
  • 1
  • 8

1 Answers1

1

This is a straightforward map operation on the entries field:

response.entries.map{ e =>
  e.modelId -> 
    if (e.metadata.get("organic").contains("true")) {
      "true-Organic"
    } else {
      "non-Organic"
    }
}

This will return List[(String, String)], but you can call toMap to turn this into a Map if required.

Tim
  • 26,753
  • 2
  • 16
  • 29