arraysjsonscala-template

How to iterate JsArray in Scala view page (value map is not a member of play.api.libs.json.JsArray)


I'm using JsArray on Scala view page, And I tried to iterate it using for loop, but throwing error on the Scala view page, Here I attached my code, how can I resolve it.

@(jsonArrValue: play.api.libs.json.JsArray)

<html>
 <body>
  <table>
   <thead>
    <tr>
     <th>Details</th>
    </tr>
   </thead>
   <tbody>
    <tr>
       @for(x <- jsonArrValue){
          <td>@x</td>
        }

    </tr>
   </tbody>
  </table>
 </body>
</html>

Error:

value map is not a member of play.api.libs.json.JsArray

jsonArrValue contains:

[{"empName":"xxx","age":"23","empNo":"123"},{"empName":"yyy","age":"24","empNo":"1234"}] 

Solution

  • While the name JsArray may suggest it's a collection sort of thing, it's actually not. The definition:

    case class JsArray(value: Seq[JsValue] = List()) extends JsValue with Product with Serializable
    

    clearly shows it. That why for comprehension does not work.

    And I think the best way to iterate it is call its value() method to convert to a Seq[JsValue], then use for comprehension.