0

Imagine you have a Map[String, List[String]] that looks like this:

val myMap = Map(
   "ab" -> List("yo", "yo2", "yo3"),
   "cd" -> List("hi", "hi1", "hi2")
 )

if we do Json.toJson(myMap), the resulting JSON is

 {
   "ab" : ["yo", "yo2", "yo3"],
   "cd" : ["hi", "hi1", "hi2"]
 }

Is there a way we could get the outer container to be an array instead of an object? Like

 [
   "ab" : ["yo", "yo2", "yo3"],
   "cd" : ["hi", "hi1", "hi2"]
 ]

I'm not sure that this would be valid Json. Thanks.

1
  • 1
    No, that is not a valid Json. You can get an array of objects: [ { "ab": [] }, { "cd": [] }]. Is it what you need? Commented Apr 12, 2016 at 15:13

1 Answer 1

1

The last one is not valid JSON. You could use Json.toJson(myMap.toList) to obtain result as @Tyth has answered. Actually Map is similar to Object of JSON format, cause it provides extracting values by key. In case with Array, it's reached over iterate over each element.

Sign up to request clarification or add additional context in comments.

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.