Converting Scala Map into JSON String
Below are the possible approaches to convert the scala map into JSON string.
Approach 1: Using Play JSON Library
- In this approach, we are using the Play JSON library in Scala to convert a Scala Map into a JSON string.
- First, we create a Scala Map with key-value pairs, and then we use Json.toJson and Json. stringify functions from Play JSON to convert the map into a JSON string.
Example:
The below example uses Play JSON Library to convert Scala Map into JSON String.
// Scala program to convert Scala Map into JSON String
import play.api.libs.json.Json
// Creating object
object GFG {
// Main method
def main(args: Array[String]): Unit = {
// Creating a map
val map = Map(
"name" -> "w3wiki",
"founder" -> "Sandeep Jain",
"Place" -> "Noida"
)
// Converting Map into JSON String
val res = Json.stringify(Json.toJson(map))
// Printing Output
println(res)
}
}
Output:
Approach 2: Using Scala Function
- In this approach, we define a custom Scala function conversionFn that takes a Scala Map as input and converts it into a JSON string using string interpolation and map operations.
- The function iterates over the map’s key-value pairs, formats them as JSON key-value pairs, and then joins them together to form a valid JSON string.
Example:
In this example, we are using a Scala function to convert a Map into a JSON String.
// Scala program to convert Scala Map into JSON String
// Creating object
object GFG {
// Function to convert Map into JSON String
def conversionFn(map: Map[String, String]): String = {
map
.map { case (key, value) => s""""$key":"$value"""" }
.mkString("{", ",", "}")
}
// Main Method
def main(args: Array[String]): Unit = {
// Creating a map
val map = Map(
"name" -> "w3wiki",
"founder" -> "Sandeep Jain",
"Place" -> "Noida"
)
// JSON String
val res = conversionFn(map)
// Printing Output
println(res)
}
}
Output:
Approach 3: Using Circle Library
- In this approach, we use the circle library to convert a Scala Map into a JSON string.
- The conversionFn function takes a Map[String, String] as input, uses asJson to convert it into a JSON structure, and then noSpaces to format it as a string without any additional spaces.
Example 3:
In this example, we use the Circe library’s asJson and noSpaces methods to convert a Scala Map into a JSON string.
// Scala program to convert Scala Map into JSON String
import io.circe._
import io.circe.syntax._
// Creating object
object MapToJson {
// Map to JSON String Conversion
def conversionFn(map: Map[String, String]): String = {
map.asJson.noSpaces
}
// Main Method
def main(args: Array[String]): Unit = {
// Creating a map
val map = Map(
"name" -> "w3wiki",
"founder" -> "Sandeep Jain",
"Place" -> "Noida"
)
// JSON String
val res = conversionFn(map)
// Printing Output
println(res)
}
}
Output:
How to Convert Scala Map into JSON String?
In this article, we will learn to convert a Scala Map into JSON String. We can convert Scala Map to JSON String using various libraries and custom conversion functions.
Table of Content
- What is Scala Map?
- What is JSON String?
- Converting Scala Map into JSON String