Search code examples
arraysjsonscalalift-json

json arrays in arrays parsing with scala and lift


Still being a noob in Scala i am hitting a wall here.

Using Scala 2.12.1 and liftweb 3.0.1

I have json code like

{
   "traces":[
      [
         {
            "matcher":"Matcher1",
            "score":{
               "assigned":50,
               "max":50
            }
         },
         {
            "matcher":"Matcher2",
            "score":{
               "assigned":50,
               "max":50
            }
         },
         {
            "matcher":"Matcher3",
            "score":{
               "assigned":50,
               "max":50
            }
         }
      ],
      [
         {
            "matcher":"Matcher4",
            "score":{
               "assigned":50,
               "max":50
            }
         },
         {
            "matcher":"Matcher5",
            "score":{
               "assigned":50,
               "max":50
            }
         },
         {
            "matcher":"Matcher6",
            "score":{
               "assigned":50,
               "max":50
            }
         }
      ]
   ]
}

I am trying to parse this to some case clases with the following lines

 case class Traces (traces: List[List[Trace]])
 case class Trace (matcher: String, score: Score)
 val result = parse(json).extract[Traces]

I know I am taking some wrong exits in my thinking but i can't seem to find a way to parse a json like this efficiently.

Any help that points me in the right direction will be greatly appreciated.

[edit] I come to understand that I did ask my question wrong here. The problem was that i did get Nil for my list of tracers. Upon further investigation I found that I did put my nested class of tracers at the wrong level of the main class. Answers and comments provided did put me in the right direction therefore upvoted and accepted.

Not sure why i got the downvotes but i guess someone's ego needed a boost by putting down a noob ;-)

Thanks for the help and the answers.


Solution

  • I'm not sure what's not working for you. Could you provide more information?

    val json =
        """
          |{
          |      "traces":
          |      [[
          |        {"matcher": "Matcher1","score": {"assigned": 50,"max": 50}},
          |        {"matcher": "Matcher2","score": {"assigned": 50,"max": 50}},
          |        {"matcher": "Matcher3","score": {"assigned": 50,"max": 50}}
          |       ],
          |       [
          |        {"matcher": "Matcher4","score": {"assigned": 50,"max": 50}},
          |        {"matcher": "Matcher5","score": {"assigned": 50,"max": 50}},
          |        {"matcher": "Matcher6","score": {"assigned": 50,"max": 50}}
          |       ]]
          |    }
        """.stripMargin
    
      import net.liftweb.json._
    
      implicit val formats = DefaultFormats
    
    
      case class Traces (traces: List[List[Trace]])
      case class Trace (matcher: String, score: Score)
      case class Score(assigned: Int, max: Int)
      val result = parse(json).extract[Traces]
    

    The above code works fine for me.