Parsing JSON and iterating through the object in Scala
Solution 1
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.
Solution 2
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
Solution 3
You could use lift-json library : http://www.assembla.com/spaces/liftweb/wiki/JSON_Support
Solution 4
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.
Solution 5
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.
José P. Airosa
As a Web Software Engineer my job is to build and enhance the new generation of web applications. Carrying with me 6+ yr PHP experience with OO and MVC development philosophy focused on backend development and supported by a strong frontend (HTML & CSS3) background. Having a keen eye to new technologies like HTML5, new applications of JavaScript and recently Scala I always thrive for a pro-active posture, adaptable to any "foreign" environment and/or development requirement. Keeping an active presence on forrst, github and as public speaker.
Updated on June 04, 2022Comments
-
José P. Airosa almost 2 years
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 over 12 yearsThank you. Very complete article, perfect for what I need.
-
José P. Airosa over 12 yearsAlready 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 over 12 yearsNice 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?
-
Tanjona over 12 yearsYes, 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
-
José P. Airosa over 12 yearsDo you find using jackson more performant than lift proprietary JSON parsing algorithm?
-
srnm over 12 yearsI 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.