3

I am newbie to Play Framework, I need to append/add JsObject elements into JsArray

Aim(What I need)

 {"s_no":1,"s_name":"one",
    ,"sub_s":  [{"sub_s_no":1,"sub_s_name":"one_sub","sub_s_desc":"one_sub"},{"sub_s_no":2,"sub_s_name":"two_sub","sub_s_desc":"two_sub"}]},
{"s_no":2,"s_name":"two","sub_s":[{"sub_s_no":2,"sub_s_name":"two_sub","sub_s_desc":"two_sub"},
    {"sub_s_no":3,"sub_s_name":"three_sub","sub_s_desc":"three_sub"}]}

What I Got

JsObject 1

{"s_no":1,"s_name":"one",
,"sub_s":[{"sub_s_no":1,"sub_s_name":"one_sub","sub_s_desc":"one_sub"},{"sub_s_no":2,"sub_s_name":"two_sub","sub_s_desc":"two_sub"}]}

JsObject 2

{"s_no":2,"s_name":"two","sub_s":[{"sub_s_no":2,"sub_s_name":"two_sub","sub_s_desc":"two_sub"},
{"sub_s_no":3,"sub_s_name":"three_sub","sub_s_desc":"three_sub"}]}

I have got two JsObject and will get more than two, I need to add/append these all JsObjects into JsArray

I tried with .+:,.append methods which gave empty JsArray values

1
  • Your "Aim" is not valid json. Also your JsObject 1 has an extra comma which means it also isn't valid json Commented Oct 14, 2015 at 19:12

1 Answer 1

4

The reason why getting an empty JsArray is because JsArray is immutable so the original JsArray will not modified. You need to assign the result of the append to a new variable in order for it to work how you expect.

val jsonString1 = """{"s_no":1,"sub_s":[1,2]}"""
val jsonString2 = """{"s_no":2,"sub_s":[3,4]}"""

val jsObj1 = Json.parse(jsonString1)
val jsObj2 = Json.parse(jsonString2)

val emptyArray = Json.arr()
val filledArray = emptyArray :+ obj1 :+ obj2

Json.prettyPrint(emptyArray)
Json.prettyPrint(filledArray)

And some of the REPL output

> filledArray: play.api.libs.json.JsArray = [{"s_no":1,"s_name":"one","sub_s":[{"sub_s_no":1,"sub_s_name":"one_sub","sub_s_desc":"one_sub"},{"sub_s_no":2,"sub_s_name":"two_sub","sub_s_desc":"two_sub"}]},{"s_no":2,"s_name":"two","sub_s":[{"sub_s_no":2,"sub_s_name":"two_sub","sub_s_desc":"two_sub"},{"sub_s_no":3,"sub_s_name":"three_sub","sub_s_desc":"three_sub"}]}]
> // pretty print of the empty array
> res1: String = [ ]
> // pretty print of the filled array
> res2: String = [ {"s_no" : 1 ...}, {"s_no" : 2 ...} ]
Sign up to request clarification or add additional context in comments.

3 Comments

JoseM, I am using for loop for getting a JsObjects as it may return any numbers based on my values. I used below code but it stores only the last JsObjects in filledArray. previous objects are not stored. val emptyArray = Json.arr() var filledArray = emptyArray for (testdata <- testList) { val testObject:JsObject = Json.obj("s_no" -> testdata) filledArray = emptyArray :+ testObject } prinln("filledArray:="+filledArray)
In for loop I used filledArray = filledArray :+ testObject instead of filledArray = emptyArray :+ testObject it solved my problem.Thanks friend.
If you have an existing Seq of JsObjects you can just do JsArray(jsObjectsSeq). If you want to add it to an existing array you can do val updatedArray = existingArray ++ JsArray(jsObjectsSeq)

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.