1

I have an array of student IDs which is dynamic in scala.

val studentIds = Seq(1,2,3,4....)

I should convert them into a JSON array in Spray JSON.

like

[
 {"student_id" : 1 },
 {"student_id" : 2 },
 {"student_id" : 3 },
]

How to do it without a case class?

2 Answers 2

3

You can use Maps. Each map will be translated directly to an json object.

import spray.json._
import DefaultJsonProtocol._

val studentIds = Seq(1,2,3,4).map(s => Map("student_id" -> s))

println(studentIds.toJson)
Sign up to request clarification or add additional context in comments.

Comments

0

You can use the below code snippet-

import spray.json.{JsNumber, JsObject}
val studentIds = Seq(1,2,3,4).map{x => JsObject("student_id" -> JsNumber(x))}
  
println(studentIds)

Comments

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.