2

I need to create the following JSON object in Scala and Play framework, but I am having trouble:

{"employees":[
  {"firstName":"John", "lastName":"Doe"}
]}

The data used to create the object comes from a html form (already implemented). So far my code creates the following:

val json: JsValue = Json.toJson(formContent)
//returns = {"firstName":"John", "lastName":"Doe"} 

How can I add the the key "employees" to this object?

Can anyone help me?

Thanks

3
  • 2
    Json.obj("employees"->Json.arr(json)) Commented Jul 20, 2016 at 19:31
  • 1
    I knew it would be something very simple but couldn't figure out. Thanks it works. Commented Jul 20, 2016 at 19:35
  • Plz Accept the answer Commented Jul 20, 2016 at 20:06

1 Answer 1

6
Json.obj("employees"->Json.arr(JSON))
Sign up to request clarification or add additional context in comments.

2 Comments

How can I update this object to add a new employee? I've tried this approach but it creates a new employee key. val newJson = json.as[JsObject] + ("employee" -> Json.toJson(list))
Json.arr(employeed1).:+(employee2)+...+(employeedn)

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.