4

Given, for example, the following JSON string:

[{"id": "user1", "password": "ps1"},{"id": "user2", "password": "ps2"},{"id": "user3", "password": "ps3"}]

What's the best and most optimized way to parse it in Scala and iterate through every result and analise it properly?

Thank you.

José P. Airosa
  • 368
  • 1
  • 2
  • 11

5 Answers5

9

with Lift-JSON:


import net.liftweb.json.JsonParser._
import net.liftweb.json.DefaultFormats

val jsonString = //your jsonString....

case class Credential (id:String, password:String)

implicit val formats = DefaultFormats
val credentials = parse(jsonString).extract[List[Credential]]

credentials foreach { cred => println(cred.id + " " + cred.password) } 

everything is explain here : http://www.assembla.com/spaces/liftweb/wiki/JSON_Support

Joni
  • 2,779
  • 23
  • 17
Tanjona
  • 91
  • 4
  • Nice concept by extracting the resulting parsed JSON object to other object. If for example, the fields do not match, or the keys from the JSON are not matchable to the ones in my Credential object I will get an error correct? – José P. Airosa Sep 14 '11 at 10:45
  • Yes, you can surround it with try catch around the parse method. I like it because it will work only if you JSonString structure is correct – Tanjona Sep 14 '11 at 13:44
6

I think this blogpost gives an comprehensive answer to your question: http://debasishg.blogspot.com/2011/02/applicatives-for-composable-json.html at the end there is also a link to a full source repository.

AndreasScheinert
  • 1,918
  • 12
  • 18
2

You could use lift-json library : http://www.assembla.com/spaces/liftweb/wiki/JSON_Support

OXMO456
  • 3,558
  • 2
  • 25
  • 35
  • Already had a look at that but I found more information on the github repo: `https://github.com/lift/lift/blob/master/framework/lift-base/lift-json/README.md` By using LINQ :) – José P. Airosa Sep 14 '11 at 10:39
1

Aside from lift-json, and the type class approach mentioned above, I know of spray-json (PEG parser), and twitter's json lib (based on the code in the Programming in Scala book) and the json lib in blueeyes. There are others!

I suggest taking a look at Jackson which is a mature and feature-rich library for JSON-parsing from Java.

Jackson has an "official" extension for scala: jackson-module-scala and another one Jerkson.

srnm
  • 83
  • 1
  • 1
  • 7
  • Do you find using jackson more performant than lift proprietary JSON parsing algorithm? – José P. Airosa Sep 14 '11 at 13:59
  • I haven't been benchmarking. However, correctness and long-term support are a priority and some of the libraries other than Jackson are lacking there. However, there are benchmarks by others that show Jackson to be very performant. – srnm Sep 15 '11 at 11:09
1

There's a JSON parsing library in the framework, built using the parser combinators: http://www.scala-lang.org/api/current/scala/util/parsing/json/package.html

Odersky and Venners walk you through it in their book, one of the last chapters.

Nick A Miller
  • 1,325
  • 1
  • 13
  • 20