αƞjiβ
αƞjiβ

Reputation: 3256

Converting List[String] to String with quote in Scala

Is there an easy way to convert List[String] to String format with " and ,. For example

val fruits: List[String] = List("Apple", "Banana", "Grapes", "Pears")
==> "Apple", "Banana", "Grapes", "Pears"

I want to do this because I have to append the result in JSON string.

{
 ....,
 "fruits": ["Apple", "Banana", "Grapes", "Pears"],
 ....
}

Upvotes: 0

Views: 3050

Answers (1)

Arnon Rotem-Gal-Oz
Arnon Rotem-Gal-Oz

Reputation: 25939

fruits.mkString("\"fruits\": [\"", "\",\"", "\"]")

but I'd use something like https://github.com/circe/circe instead of building JSONs myself

Upvotes: 6

Related Questions