Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the most straightforward way to parse JSON in Scala?

I'm working on a simple web application with Scala. The plan is to obtain JSON data from an external API, and insert it into a template (unfortunately, obtaining the data in XML is not an option).

I've tried working with Twitter's scala-json library, but I can't get it to compile properly (the code on github fails to update in sbt, saying standard-project 7.10 is not available and I haven't worked that out yet).

lift-json looks impressive, but appears to be a lot more elaborate than I need right now.

Trying to import a library I've worked with in Java, jsonic, results in various arcane errors. This is too bad because I rather like how straightforward jsonic is.

I've made a bit of progress with the built in scala.util.parsing.json.JSON, but actually I can't tell how to access the elements. I'm somewhat new to Scala, as you may have noted. How do you access the properties of JSONObjects?

scala.util.parsing.json.JSON has a lot of information, but is there a straightforward tutorial on how to use this anywhere?

I'm really only interested in deserializing JSON at the moment, to Ints, Strings, Maps and Lists. I don't have a need to serialize objects or make the deserialized objects fit into a class at the moment.

Can anyone point me to ways to work with one of the aforementioned libraries, or help me get set up with a Java lib that will do what I want?

like image 286
JAL Avatar asked Nov 12 '10 21:11

JAL


People also ask

What is the best JSON library for Scala?

Why? Argonaut is a great library. It's by far the best JSON library for Scala, and the best JSON library on the JVM. If you're doing anything with JSON in Scala, you should be using Argonaut.

What is the best JSON parser?

It's the most popular JSON parser, according to our findings on Github usages. Oracle's JSONP: https://jsonp.java.netJSONP (JSON Processing) is a Java API for JSON processing, namely around consuming and producing streaming JSON text. It's the open source reference implementation of JSR353.

What is the fastest JSON parser?

We released simdjson 0.3: the fastest JSON parser in the world is even better! Last year (2019), we released the simjson library. It is a C++ library available under a liberal license (Apache) that can parse JSON documents very fast.


1 Answers

Lift JSON provides several different styles of deserializing JSON. Each have their pros and cons.

val json = JsonParser.parse(""" { "foo": { "bar": 10 }} """)

LINQ style query comprehension:

scala> for { JField("bar", JInt(x)) <- json } yield x 

res0: List[BigInt] = List(10)

More examples: http://github.com/lift/lift/blob/master/framework/lift-base/lift-json/src/test/scala/net/liftweb/json/QueryExamples.scala

Extract values with case classes

implicit val formats = net.liftweb.json.DefaultFormats 
case class Foo(foo: Bar) 
case class Bar(bar: Int) 
json.extract[Foo] 

More examples: https://github.com/lift/lift/blob/master/framework/lift-base/lift-json/src/test/scala/net/liftweb/json/ExtractionExamples.scala

XPath style

scala> val JInt(x) = json \ "foo" \ "bar"

x: BigInt = 10

Non-type safe values

scala> json.values

res0: Map((foo,Map(bar -> 10)))
like image 190
Joni Avatar answered Oct 15 '22 19:10

Joni