Swapnil Swapnil - 3 months ago 20
Scala Question

scala iterate over JSON Array

I have a very simple JSON as below and I want to get

sJson[0], sJson[1]
objects, How can I achieve this ?

scala> val jsonA = "[{'foo': 4, 'bar': 'baz'},{'bla': 4, 'bar': 'bla'}]"
jsonA: String = [{'foo': 4, 'bar': 'baz'},{'bla': 4, 'bar': 'bla'}]

scala> val sJson = parseFull(jsonA)
sJson: Option[Any] = None

scala> println(sJson.toString())
None


enter image description here

Answer

JSON notation wants you to surround variables with double quotes:

val jsonA = """[{"foo": 4, "bar": "baz"},{"bla": 4, "bar": "bla"}]"""
val sJson = parseFull(jsonA).get // List(Map(foo -> 4.0, bar -> baz), Map(bla -> 4.0, bar -> bla))