montefuscolo
montefuscolo

Reputation: 269

json4s error: value \\ is not a member of (String, org.json4s.JsonAST.JObject)

First of all, I am very new to Scala, so forgive me for eventual stupid mistakes.

In the Json4s Readme there is the following code:

scala> import org.json4s.jackson.JsonMethods._
scala> import org.json4s.JsonDSL._


scala> val json =
  ("person" ->
  ("name" -> "Joe") ~
  ("age" -> 35) ~
    ("spouse" ->
    ("person" ->
     ("name" -> "Marilyn") ~
     ("age" -> 33)
    )
  )
)

scala> json \\ "spouse"
res0: org.json4s.JsonAST.JValue = JObject(List(
      (person,JObject(List((name,JString(Marilyn)), (age,JInt(33)))))))

I get the following error when I run this code:

error: value \\ is not a member of (String, org.json4s.JsonAST.JObject)
json \\ "spouse"
     ^

My sbt file is the following:

name := "Impressions"
version := "1.0"
scalaVersion := "2.10.6"

libraryDependencies ++= Seq(
  "org.apache.spark" %% "spark-core" % "1.4.1" % "provided",
  //"org.json4s"  %% "json4s-native" % "3.3.0"
  "org.json4s"  %% "json4s-jackson" % "3.3.0"
)

and I run the example under sbt console. Any thoughts?

Upvotes: 2

Views: 2432

Answers (1)

mfirry
mfirry

Reputation: 3692

If you slightly modify the json declaration like this:

val json: JObject =
  ("person" ->
  ("name" -> "Joe") ~
  ("age" -> 35) ~
    ("spouse" ->
    ("person" ->
     ("name" -> "Marilyn") ~
     ("age" -> 33)
    )
  )
)

...it will work.

I think the compiler assumes you're declaring a tuple with a String and a JObject, while you actually want to declare a full JObject.

Upvotes: 1

Related Questions