1

now I'm trying to parse json to case class, and I have a problem. This is my json string:

{"book_id":"1", "book_name":"Skype", "author_name":"bla bla", "author_country":"Poland"}

And I have 2 case classes:

case class Book(bookId: String, bookName: String){}
case class Author(authorNam: String, authorCountry: String){}

So how can I parse the Json into 2 classes? The output will be:

Book(1, Skype)
Author(bla bla, Poland)

Thank you so much for your help.

3 Answers 3

2

There are a few solutions for you, the most obvious of them:

  1. Json4s (it supports jackson)

  2. Spray-Json is a lightweight, clean and efficient JSON implementation in Scala.

UPD Let's consider usage of Json4s:

  object JsonExample extends App {

  import org.json4s._
  import org.json4s.JsonDSL._
  import org.json4s.jackson.JsonMethods._

  case class Winner(id: Long, numbers: List[Int])
  case class Lotto(id: Long, winningNumbers: List[Int], winners: List[Winner], drawDate: Option[java.util.Date])

  val winners = List(Winner(23, List(2, 45, 34, 23, 3, 5)), Winner(54, List(52, 3, 12, 11, 18, 22)))
  val lotto = Lotto(5, List(2, 45, 34, 23, 7, 5, 3), winners, None)

  val json =
    ("lotto" ->
      ("lotto-id" -> lotto.id) ~
      ("winning-numbers" -> lotto.winningNumbers) ~
      ("draw-date" -> lotto.drawDate.map(_.toString)) ~
      ("winners" ->
        lotto.winners.map { w =>
          (("winner-id" -> w.id) ~
           ("numbers" -> w.numbers))}))

  println(compact(render(json)))
}
Sign up to request clarification or add additional context in comments.

4 Comments

Yes, but my gold is mapping "book_id" and "bookId".
Chech my answer, (upd section), ` ("lotto-id" -> lotto.id)` is similiar to your example.
I checked, and your answer is render the Json, I need to parse from Json to case class.
This example solves your problem with "book_id" and "bookId", just use parsing instead rendering. The Idea of my answer is in DSL specified by Json4s. Of course, you can find similar to your example om main page of Json4s on github - first link provided by me.
2

And this is the answer:

object JsonExample extends App {

  import org.json4s._
  import org.json4s.JsonDSL._
  import org.json4s.jackson.JsonMethods._

  implicit val formats = DefaultFormats // Brings in default date formats etc.

  case class Book(bookId: String, bookName: String) {}
  case class Author(authorName: String, authorCountry: String) {}

  val jsonString = """{"book_id":"1", "book_name":"Skype", "author_name":"bla bla", "author_country":"Poland"}"""
  val json = parse(jsonString)
  println(json.camelizeKeys.extract[Book])
  println(json.camelizeKeys.extract[Author])
}

Thank @rukavitsya for this.

Comments

-1

Well, you could create another class to get the full Json, as a response class:

case class Response(bookId: String, bookName: String, authorNam: String, authorCountry: String){}

You map your response, that i am assuming comes from a server or other service, and then from this you separate into your two other classes, mapping them.

The better you can do in these cases is getting the whole response that you'll use in your use case, or in your code in general, and then treating the data in your code, before displaying or using the received data.

EDIT: to map the response, using your example, you'll have

Response(1, Skype, bla bla, Poland)

then you would do something like

Book(Response.bookID, Response.bookName)
Author(Response.authorName, Response.authorCountry)

Other approach is having your Response being a case class of the other classes, nesting the case classes, so you'll have both classes inside your Response.

Hope it helps. Cheers!

3 Comments

Thank you, but I sorry because this example is not exactly, the total properties of case class Book and Author more than 22 fields, so we can not create new case class with all properties. And my gold is how to mapping "book_id" to "bookId"
well, in your question, it seemed that the problem was parsing to 2 different classes... you can always use frameworks for serialization. it maps a given field to the variable you want. A framework you can use is json4s: github.com/json4s/json4s
Yes, thank you. I checked the json4s but I still don't understand how to map "book_id" to "bookId", can you explain more or can you give me a example?

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.