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.

5 Answers 5

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

Sign up to request clarification or add additional context in comments.

2 Comments

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?
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
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.

1 Comment

Thank you. Very complete article, perfect for what I need.
2

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

1 Comment

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 :)
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.

2 Comments

Do you find using jackson more performant than lift proprietary JSON parsing algorithm?
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.
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.

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.