I am new to Scala and I want to build an object that looks like below and then convert to Json, but doing something like this gives me error. How can I achieve this?
This is what I tried:
Map(a -> Map(b-> "1", c -> "2"),
Map(d -> values.map(v => Map(b-> v.value1, c -> v.value2)))
.asJson
Expected outcome:
{
"a":{"b": "1", "c": "2"},
"d":[{"b":"x1","c":"x2"},{"b":x3,"c":"x4"}...]
}
You should reserve to using the JSON library's provided DSL instead, i.e. circe JSON DSL:
import io.circe.Json
import io.circe.syntax.*
Json.obj(
"a" := Json.obj("b" := "1", "c" := "2")
"d" := values.map(v => Json.obj("b" := v.value1, "c" := v.value2))
)
Related
I face following problem.
I have this map where key represents an element and the value represents its parent element:
Map(
"a1" -> Some("b1"),
"a2" -> Some("b1"),
"b1" -> Some("c1"),
"c1" -> None
)
I would need to create something like "generation sequences", which would look something like this:
Seq(
Seq("a1", "b1", "c1"),
Seq("a2", "b1", "c1")
)
How can I do it, especially so I wouldn't left with this:
Seq(
Seq("a1", "b1", "c1"),
Seq("b1", "c1"),
Seq("c1"),
Seq("a2", "b1", "c1"),
Seq("b1", "c1"),
Seq("c1")
)
Thank you for answers!
I have a Scala List of Map[String, String] like this:
val data: List[Map[String, String]] = List(Map("key" -> "123", "fname" -> "Alice", "lname" -> "Baker"), Map("key" -> "456", "fname" -> "Bob", "lname" -> "Lotts"))
I want to transform this to a List like this: List(Map(id -> 123, name -> Alice Baker), Map(id -> 456, name -> Bob Lotts)). Basically, I need to change the key to id and concatenate the fname and lname to name.
I tried the below code. It works, but I am sure there should be a better way of doing this. Can anyone please suggest?
val modData: List[Map[String, String]] = data.map(d => Map("id" -> d.getOrElse("key", ""), "name" -> s"${d.getOrElse("fname", "")} ${d.getOrElse("lname", "")}"))
I would do it in steps, and use default for the map to make it more readable:
val keys = Seq("key", "fname", "lname")
list.iterator
.map(_.withDefault(_ => ""))
.map(keys.map)
.collect { case Seq(id, fname, lname) => Map("id" -> id, "name" -> s"$fname $lname") }
.toList
val mapa = Map("a" -> Array(Map("b" -> "c", "d" -> Array("e"))))
val mapa2 = Map("a" -> Array(Map("b" -> "c", "d" -> Array("e"))))
Is there way how to get key and value from both same maps and compare them?
or how to get all key from map with such structure?
I have this Seq[Map[String, String]] :
val val1 = Seq(
Map("Name" -> "Heidi",
"City" -> "Paris",
"Age" -> "23"),
Map(("Country" -> "France")),
Map("Color" -> "Blue",
"City" -> "Paris"))
and I have this Seq[String]
val val2 = Seq["Name", "Country", "City", "Department"]
Expected output is val1 with all keys present in val2 (I want to filter out the (k,v) from v1 that have keys that are not present in val2) :
val expected = Seq(Map("Name" -> "Heidi", "City" -> "Paris"), Map( "Country" -> "France")), Map("City" -> "Paris"))
Age and Color are strings that are not in val2, I want to omit them from val1 map.
I'm not sure if what you propose is a right approach but nevertheless, it can be done like this:
val1.map(_.filter {
case (key, value) => val2.contains(key)
})
It seems you want something like this:
(note that I used a Set instead of a List to make contains faster)
def ensureMapsHaveOnlyValidKeys[K, V](validKeys: Set[K])(data: IterableOnce[Map[K, V]]): List[Map[K, V]] =
data
.iterator
.filter(_.keysIterator.forall(validKeys.contains))
.toList
I am writing below code,
val maplist=List(Map("id" -> "1", "Name" -> "divya"),
Map("id" -> "2", "Name" -> "gaya")
)
val header=maplist.flatMap(_.keys).distinct
val data=maplist.flatMap(_.values)
println(header)
println(data)
I am getting the below output,
List(id, Name)
List(1, divya, 2, gaya)
however I am expecting output as below,
id Name
1 Divya
2 gaya
here in this case I am having only 2 header but in my map it may contain more than 2 headers how to display all in rows. Please help me.
val maplist=List(Map("id" -> "1", "Name" -> "divya"),
Map("id" -> "2", "Name" -> "gaya")
)
val header=maplist.flatMap(_.keys).distinct
val data=maplist.map(_.values)
println(header.mkString(" "))
data.foreach(x => println(x.mkString(" ")))