1

What is the best way to convert Json Array to scala list, and also Json list is list of JsonObject and they don't have any class in my scala code I don't need it, there my simple json string

val jsonData = """{
                       |    "store": {
                       |        "book": [
                       |            {
                       |                "category": "reference",
                       |                "author": "Nigel Rees",
                       |                "title": "Sayings of the Century",
                       |                "price": 8.95
                       |            },
                       |            {
                       |                "category": "fiction",
                       |                "author": "Evelyn Waugh",
                       |                "title": "Sword of Honour",
                       |                "price": 12.99
                       |            },
                       |            {
                       |                "category": "fiction",
                       |                "author": "Herman Melville",
                       |                "title": "Moby Dick",
                       |                "isbn": "0-553-21311-3",
                       |                "price": 8.99
                       |            },
                       |            {
                       |                "category": "fiction",
                       |                "author": "J. R. R. Tolkien",
                       |                "title": "The Lord of the Rings",
                       |                "isbn": "0-395-19395-8",
                       |                "price": 22.99
                       |            }
                       |        ],
                       |        "bicycle": {
                       |            "color": "red",
                       |            "price": 19.95
                       |        }
                       |    },
                       |    "expensive": 10
                       |}""".stripMargin

and I have input json

{   "hello": "$.store.book[?(@.price < 10)]" }

which say get from above json list which satisfied to this criteria

there my class which do all I need things:

class CustomConductor {

  def extractRequiredObject[T](jsonString: String)(implicit m: Manifest[T]): T = {
    extractFrom(jsonString) match {
      case Success(jsonParsed) =>
        jsonParsed
      case Failure(exc) =>
        throw new IllegalArgumentException(exc)
    }
  }

  def parsing(headerData: mutable.Map[String, String], jobData: String): Map[String, Any] = {
    var t: Map[String, Any] = Map()
    headerData.foreach{data: (String, Any) =>
      t += (data._1 -> recurse(parse(data._2.toString).values.asInstanceOf[Map[String, Any]], jobData, data._2.toString))
    }
    t
  }

  private def recurse(m: Map[String, Any], data: String, myMap: String): Map[String, Any] = {

    def helper(m: Map[String, Any]): Map[String, Any] = {
      var b = m
      m.foreach(k => {
        k._2 match {
          case str: String if str.startsWith("$") =>
            val res = getDataByJsonPath(data, k._2.toString)
            if (res != null) {
// here I want to check if res is JsonArray of object convert it to List of Map
//              if (res.isInstanceOf[JSONArray]) {
//                val json = parse(res.toString)
//                for (elem <- json.children) {
//                  println(elem)
//                }
//                b += (k._1 -> json.children.toList)
//              }
              b += (k._1 -> res)
            }
          case map: Map[String, Any] =>
            b += (k._1 -> helper(map))
          case _ =>
        }
      })
      b
    }
    helper(m)
  }

  private def extractFrom[T](jsonString:String)(implicit m: Manifest[T]): Try[T] = {
    implicit val formats: DefaultFormats.type = DefaultFormats

    Try {
      parse(jsonString).extract[T]
    }
  }

  private def getDataByJsonPath(jsonString: String, jsonPath: String): Any = {
    val a = JsonPath.read[Any](jsonString, jsonPath)
    if (a.isInstanceOf[JSONArray]) {


      JsonPath.read[List[Any]](jsonString, jsonPath)
    }else
      a
  }

}

converting to the map work normally, but as you see hello value is not List it JsonArray

Map(hello->[
  {
    "author": "Nigel Rees",
    "price": 8.95,
    "category": "reference",
    "title": "Sayings of the Century"
  },
  {
    "author": "Herman Melville",
    "price": 8.99,
    "isbn": "0-553-21311-3",
    "category": "fiction",
    "title": "Moby Dick"
  }
])))

1 Answer 1

1

If you don't have and don't need mapping it to case classes, you can use simplest library I know ujson.

more on: http://www.lihaoyi.com/post/uJsonfastflexibleandintuitiveJSONforScala.html

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.