1

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

3
  • Which library do you use for json parsing? Commented Aug 17, 2016 at 10:48
  • parseFull is from the scala.util.parsing.json.JSON package Commented Aug 17, 2016 at 10:52
  • Yup, scala.util.parsing.j‌​son.JSON package. I just want that sJson 's obj[0], obj[1] .. etc Commented Aug 17, 2016 at 11:23

2 Answers 2

2

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.asInstanceOf[List[Map[String,String]]] // List(Map(foo -> 4.0, bar -> baz), Map(bla -> 4.0, bar -> bla))
Sign up to request clarification or add additional context in comments.

7 Comments

Get some exceptions scala> val sJson = parseFull(jsonA).get java.util.NoSuchElementException: None.get at scala.None$.get(Option.scala:347) at scala.None$.get(Option.scala:345)
hmm, what is your jsonA? the one I posted in the answer is parsing fine.
I see single quotes around the variables in your JSON in the image you just posted.
lol. Single quotes was the problem. Thanks Samar. It's working !
Samar, could you please do one more favour for me, my real quetion was to get first object from the JSON or list. I'm getting scala> sJson(0) <console>:23: error: Any does not take parameters sJson(0) error. :(
|
2

While the scala.util.parsing.j‌son.JSON works well it is best to use an external library like json4s which has a fluent scala api.

You can query json just like you query xml (like JsonPath).

Below are the steps how you can use json4s for your need once you have it in your project.

First add the following imports

import org.json4s._
import org.json4s.native.JsonMethods._

Now create a Jvalue by doing

val jsonA = """[{"foo": 4, "bar": "baz"},{"bla": 4, "bar": "bla"}]"""
val json = parse(jsonA)

Your json object is as

json: org.json4s.JValue = JArray(List(JObject(List((foo,JInt(4)), (bar,JString(baz)))), JObject(List((bla,JInt(4)), (bar,JString(bla))))))

To get the first (0th) child

val firstChild = (json)(0)

firstChild: org.json4s.JsonAST.JValue = JObject(List((foo,JInt(4)), (bar,JString(baz))))

To get a string representation of this firstChild

val firstChildString = compact(render(firstChild))

firstChildString: String = {"foo":4,"bar":"baz"}

See the section Querying JSON in json4s home page for more.

Note: to import the jars you can use maven/gradle or import them into scala repl by using :require command.

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.