How to Create a JSON Object in Scala? Last Updated : 28 Mar, 2024 Comments Improve Suggest changes Like Article Like Report JSON(JavaScript Object Notation) plays an important role in web development and data transmission. In web applications, we frequently use JSON, a lightweight data transmission standard. Working with JSON data is made easy using Scala, a sophisticated programming language for the Java Virtual Machine (JVM). This article focuses on discussing how to create a JSON object in Scala. Here are a few important applications of JSON in Scala: Data transmissionSerialization and DeserializationData ProcessingData AnalysisTestingFrontend - Backend configurationsExample 1: The below Scala program implements how to create a JSON object. Scala object Main{ def main(args: Array[String]) { val jObject = Map( "Name" -> "Jimin", "Age" -> 26, "City" -> "Seoul" ); val jString = mapToJsonString(jObject); println(jString); }; def mapToJsonString(map: Map[String, Any]): String = { val ele = map.map { case (key, value) => "\"" + key + "\":" + valueToJsonString(value); }; "{" + ele.mkString(",") + "}"; }; def valueToJsonString(value: Any): String = value match { case s: String => "\"" + s + "\""; case _ => value.toString; }; }; Output{"Name":"Jimin","Age":26,"City":"Seoul"} Explanation: In the above code, We have Created a JSON - like map i.e., val jObject = Map(). Next we have Converted into to JSON - like string i.e., val jString = mapToJsonString(jObject). Above step calls for a function mapToJsonString. This function takes a Map[String, Any] and converts it into a JSON-like string.It iterates over map and constructs a string in format of key-value pair using the valueToJsonString function. valueToJsonString Function converts a value to a JSON string using toString Method. Later we have printed the jString. Example 2: In this example, Let us use built-in scala.collection.mutable.Map data structure. This data structure helps us to create a key - value pairs like a JSON object. Scala import scala.collection.mutable.Map; object Main { def main(args: Array[String]) { val jObject = Map( "Name" -> "Jimin", "Age" -> 26, "Contact" -> "9xxx5xxx35" ).withDefaultValue(""); println(jObject("Name")); println(jObject("Age")); println(jObject("Contact")); } } OutputJimin 26 9xxx5xxx35 Explanation: In the above Example we have imported a data structure scala.collection.mutable.Map. We have Created a JSON - like map i.e., val jObject = Map(). withDefaultValue("") is used to provide default values for keys which are not explicitly defined. We can access the values of a JSON object using keys. Comment More infoAdvertise with us Next Article How to Create a JSON Object in Scala? 20b01a12e6 Follow Improve Article Tags : Scala Similar Reads How to mock object in Scala? In software development, mocking is a technique used to isolate the unit under test by replacing external dependencies with simulated objects or "mocks". Mocking is particularly useful when working with objects that are difficult to set up or have side effects, such as databases, web services, or fi 3 min read How to Convert JSON String to a JSON Object in Scala? When working with JSON data in Scala, we may often need to convert a JSON string into a JSON object. This can be useful for parsing and manipulating JSON data effectively. This article focuses on discussing ways to convert JSON string to a JSON object. Table of Content Using the Built-in Parse Metho 3 min read How to print dataframe in Scala? Scala stands for scalable language. It was developed in 2003 by Martin Odersky. It is an object-oriented language that provides support for functional programming approach as well. Everything in scala is an object e.g. - values like 1,2 can invoke functions like toString(). Scala is a statically typ 4 min read How to read and write JSON files in Scala? Scala is frequently used for reading and writing JSON files in a variety of applications, particularly it includes data transmission.Table of ContentSteps for reading JSON files in Scala:Steps for writing JSON files in Scala:Steps for reading JSON files in Scala:When reading JSON files in Scala we f 3 min read Class and Object in Scala Classes and Objects are basic concepts of Object Oriented Programming which revolve around the real-life entities. Class A class is a user-defined blueprint or prototype from which objects are created. Or in other words, a class combines the fields and methods(member function which defines actions) 5 min read Like