I want to convert a simple JSON string such as {"Name":"abc", "age":10}
to the corresponding JSON object (not a custom Scala object such as "Person"). Does Scala support any in-built methods to convert a String to a JSON object?
I'm not going to have any complex JSON operations. I just need to convert the String to a JSON object. What is the simplest way to do this? I'm new to Scala, so I apologize if this question sounds very basic.
Thanks.
-
There are many JSON lib in Scala. Each of they provide a parse function to get JSON value from a string. You first need to choose a lib (Play JSON, Argonaut, ...).cchantep– cchantep2015年06月17日 07:29:55 +00:00Commented Jun 17, 2015 at 7:29
-
@cchantep Thx, but I wanted to avoid using external library if possible. That's why I wanted to know if Scala has some built-in support. Guess I'll just have to use a library then.drunkenfist– drunkenfist2015年06月17日 07:36:33 +00:00Commented Jun 17, 2015 at 7:36
5 Answers 5
Note: Technically, there is no longer a core Scala "native" way of parsing JSON. You should use an external, supported library like Spray JSON or Play JSON.
As of Scala 2.11 the parser-combinator library is no longer included in the core language jar and needs to be added separately to your project. Further, the JSON parser has since been deprecated in the community supported version of the parser-combinator library. I would not recommend using this library.
You can still add it to your project, if you choose to, by adding the following to your build.sbt:
libraryDependencies += "org.scala-lang.modules" %% "scala-parser-combinators" % "1.0.4"
You can find the source code for the library at https://github.com/scala/scala-parser-combinators.
Since you asked specifically about Scala's native facilities for JSON parsing – the package you are looking for is the scala.utils.parsing.json. Something like the following should work:
import scala.util.parsing.json._
val parsed = JSON.parseFull("""{"Name":"abc", "age":10}""")
parsed
will take on the value: Some(Map(Name -> abc, age -> 10.0))
4 Comments
@scala.deprecated("This object will be removed.", "2.11.0") object JSON extends scala.util.parsing.json.Parser {
Also you can use Json Library from play framework, but can be used as standalone lib also. This library based on good but abandoned Jerkson project, which is a Scala wrapper around the super-fast Java based JSON library, Jackson. And it has very rich and good documented toolset for working with JSON - transofrmers, validators and etc.
import play.api.libs.json._
val json: JsValue = Json.parse("""{"a":1}""")
To use this lib without play just install it in build.sbt with string
libraryDependencies += "com.typesafe.play" %% "play-json" % "2.3.0"
Comments
You might want to use a library like Spray JSON. It provides a lot of easy to use functionality for converting to and from JSON. If you decide to use Spray JSON you can do this:
import spray.json._
// some code here
val json = "your json string here".parseJson
The parseFull
returns in-terms of Some(Map)
, parseRaw
returns in terms of Some(JSONObject)
import scala.util.parsing.json._
val parsed = JSON.parseRaw("""{"Name":"abc", "age":10}""").getOrElse(yourDefault)
parsed
is the JSONObject
1 Comment
@scala.deprecated("This object will be removed.", "2.11.0") object JSON extends scala.util.parsing.json.Parser {
Didn't find any recommendation about dijon (that uses jsoniter-scala under the hood).
Add the following to your build.sbt:
libraryDependency += "me.vican.jorge" %% "dijon" % "0.6.0" // Use %%% instead of %% for Scala.js
Turn on support of dynamic types by adding import clause:
import scala.language.dynamics._
or by setting the scala compiler option:
scalacOptions += "-language:dynamics"
Add import of the package object of dijon for the main functionality:
import dijon._
Optionally, add import of package object of jsoniter-scala-core for extended json functionality:
import com.github.plokhotnyuk.jsoniter_scala.core._
Parse and access parsed data:
val json = parse("""{"Name":"abc", "age":10}""")
println(json.age)