Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert Scala Map into JSON String?

Tags:

For example, I have this Map value in Scala:

val m = Map(     "name" -> "john doe",      "age" -> 18,      "hasChild" -> true,      "childs" -> List(         Map("name" -> "dorothy", "age" -> 5, "hasChild" -> false),         Map("name" -> "bill", "age" -> 8, "hasChild" -> false)     ) ) 

I want to convert it to its JSON string representation:

{     "name": "john doe",     "age": 18,     "hasChild": true,     "childs": [         {             "name": "dorothy",             "age": 5,             "hasChild": false         },         {             "name": "bill",             "age": 8,             "hasChild": false         }     ] } 

I'm currenly working on Play framework v2.3, but the solution doesn't need to use Play JSON library, although it will be nice if someone can provide both Play and non-Play solution.

This is what I have done so far without success:

// using jackson library val mapper = new ObjectMapper() val res = mapper.writeValueAsString(m) println(res) 

Result:

{"empty":false,"traversableAgain":true} 

I don't understand why I got that result.

like image 898
null Avatar asked Jan 14 '15 16:01

null


People also ask

Can we convert map to JSON?

We can convert a Map to JSON object using the toJSONString() method(static) of org. json. simple. JSONValue.

What is map in JSON?

A JSONObject is an unordered collection of name/value pairs whereas Map is an object that maps keys to values. A Map cannot contain duplicate keys and each key can map to at most one value. We need to use the JSON-lib library for serializing and de-serializing a Map in JSON format.

How do I convert JSON map to Jackson?

Converting Map to JSONHashMap<String, String> hashmap = new HashMap<String, String>(); hashmap. put("id", "1"); hashmap. put("firstName", "Lokesh"); hashmap. put("lastName", "Gupta"); ObjectMapper mapper = new ObjectMapper(); String json = mapper.


2 Answers

As a non play solution, you can consider using json4s which provides a wrapper around jackson and its easy to use. If you are using json4s then you can convert map to json just by using:

write(m)                                         //> res0: String = {"name":"john doe","age":18,"hasChild":true,"childs":[{"name":"dorothy","age":5,"hasChild":false},{"name":"bill","age":8,"hasChild":false}]} 

--Updating to include the full example--

import org.json4s._ import org.json4s.native.Serialization._ import org.json4s.native.Serialization implicit val formats = Serialization.formats(NoTypeHints)   val m = Map(   "name" -> "john doe",   "age" -> 18,   "hasChild" -> true,   "childs" -> List(     Map("name" -> "dorothy", "age" -> 5, "hasChild" -> false),     Map("name" -> "bill", "age" -> 8, "hasChild" -> false)))   write(m) 

Output:

 res0: String = {"name":"john doe","age":18,"hasChild":true,"childs":[{"name"   :"dorothy","age":5,"hasChild":false},{"name":"bill","age":8,"hasChild":false }]} 

Alternative way:

import org.json4s.native.Json import org.json4s.DefaultFormats  Json(DefaultFormats).write(m) 
like image 65
mohit Avatar answered Nov 01 '22 08:11

mohit


val mapper = new ObjectMapper() mapper.writeValueAsString(Map("a" -> 1)) 

result> {"empty":false,"traversableAgain":true}

==============================

import com.fasterxml.jackson.module.scala.DefaultScalaModule  val mapper = new ObjectMapper() mapper.registerModule(DefaultScalaModule) mapper.writeValueAsString(Map("a" -> 1)) 

result> {"a":1}

like image 29
forcontents Avatar answered Nov 01 '22 08:11

forcontents