0

Here is my Json:

{
  "root": {
    "qr": {
      "content": "V1"
    },
    "datag": {
      "content": {

        "business": {
          "content": [
            {
              "content": "car"
            },
            {
              "content": "bike"
            }
          ]
        }

      }
    }
  }
}

Here is my attempt but i receive compilation error:

implicit val reads: Reads[rules] = (
   (JsPath \ "content" \ "qr" \ "content").readNullable[String] and
    (JsPath \ "content" \ "datag" \ "content" \ "business" \ "content").readNullable[Seq[String]]
 )(rules.apply _)   

What's the best way to parse it as a list of string?

3
  • What's the exact desired output? Commented Feb 23, 2018 at 12:30
  • A list ["car","bike"] Commented Feb 23, 2018 at 13:01
  • Can you detail what is your rules case class and what is your compilation error ? Commented Feb 23, 2018 at 14:28

2 Answers 2

1

If you want your answer as a List("car","bike") as per your comment and you are looking for a one liner, you can try this

val json = Json.parse("""{
 |   "root": {
 |     "qr": {
 |       "content": "V1"
 |     },
 |     "datag": {
 |       "content": {
 | 
 |         "business": {
 |           "content": [
 |             {
 |               "content": "car"
 |             },
 |             {
 |               "content": "bike"
 |             }
 |           ]
 |         }
 | 
 |       }
 |     }
 |   }
 | }""")

val contents = (json \ "root" \ "datag" \ "content" \ "business" \ "content" ).as[List[Map[String, String]]].flatMap(_.values)
//this can throw an error if the data under 'content' is not a List
val contentsNew = (json \ "root" \ "datag" \ "content" \ "business" \ "content" ).asOpt[List[Map[String, String]]].map(_.flatMap(_.values)).fold(List.empty[String])(identity)
//this one is safe option 

or but I would suggest to create a case class and then use Json.format[CaseClass] you can take a look at this link.

Sign up to request clarification or add additional context in comments.

Comments

0

You can use the scala json parser to convert the json string into Map[String,Any] then get the value using key.

import scala.util.parsing.json._
val jsonString =
    """{
      |  "root": {
      |    "qr": {
      |      "content": "V1"
      |    },
      |    "datag": {
      |      "content": {
      |
      |        "business": {
      |          "content": [
      |            {
      |              "content": "car"
      |            },
      |            {
      |              "content": "bike"
      |            }
      |          ]
      |        }
      |
      |      }
      |    }
      |  }
      |}""".stripMargin

function to iterate complex json for give key list and get inner json value.

def getValue(input: Map[String, Any], keys: List[String]): Any = keys match {
    case lastKey :: Nil => input(lastKey)
    case key :: _ => getValue(input(key).asInstanceOf[JSONObject].obj, keys.tail)
  }

finally parse the json and get the output:

JSON.parseRaw(jsonString) match {
    case Some(jsonVal) =>
      println(jsonVal)
      val jsonMapKeyValuePair: Map[String, Any] =
        jsonVal.asInstanceOf[JSONObject].obj
      val keys = List("root", "datag", "content", "business", "content")
      val output: List[Any] =
        getValue(jsonMapKeyValuePair, keys)
          .asInstanceOf[JSONArray]
          .list
          .map(_.asInstanceOf[JSONObject].obj)
          .map(_.get("content").get)
      println(output)
    case _ =>
      println("Invalid Json Object.")
  }

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.