// Reads Json file
val input_file = ("\\path\\to\\MyNew.json");
val json_content = scala.io.Source.fromFile(input_file).mkString
// parsing the json file
val details = JSON.parseFull(json_content)
// checking the matched result
details match {
case mayBeList: Some[Map[String, Any]] =>
val z = mayBeList.get.tails.toSet.flatten
z.foreach(println)
case None => println("Parsing failed")
case other => println("Unknown data structure: " + other)
}
getting following Output:
Map(Name -> Harish, Company -> In Equity, Sal -> 50000)
Map(Name -> Veer, Company -> InOut, Sal -> 20000)
Map(Name -> Zara, Company -> InWhich, Sal -> 90000)
Map(Name -> Singh, Company -> InWay, Sal -> 30000)
Map(Name -> Chandra, Company -> InSome, Sal -> 60000)
Expected Output
Harish, In Quality, 50000- (only values of Map)
USB
6,15915 gold badges69 silver badges96 bronze badges
3 Answers 3
Use .values
for the values and .keys
for the keys.
val m: Map[String, Int] = Map("a" -> 1, "b" -> 2)
m.values // res0: Iterable[Int] = MapLike(1, 2)
m.keys // res1: Iterable[String] = Set(a, b)
answered Dec 18, 2018 at 13:18
2 Comments
James Whiteley
Obviously you can convert these to a List or Seq or whatever with the relevant
.to...
method (eg .toList
, toSeq
, etc).Vishal Reddy
Hi James thanks for your reply, but its throwing an error for me.
All you need is to iterate though the elements of your list
i.e. z
and extract values from each map
like this,
List(Map("Name" -> "Harish", "Company" -> "In Equity", "Sal" -> 50000),
Map("Name" -> "Veer", "Company" -> "InOut", "Sal" -> 20000),
Map("Name" -> "Zara", "Company" -> "InWhich", "Sal" -> 90000),
Map("Name" -> "Singh", "Company" -> "InWay", "Sal" -> 30000),
Map("Name" -> "Chandra", "Company" -> "InSome", "Sal" -> 60000)
)
.map(_.values.toList).foreach(println)
//List[List[Any]] = List(List(Harish, In Equity, 50000), List(Veer, InOut, 20000), List(Zara, InWhich, 90000), List(Singh, InWay, 30000), List(Chandra, InSome, 60000))
Hope this helps you.
Update
In response to your comment, use this code
import scala.util.parsing.json._
val input_file = ("C:\\Users\\VishalK\\IdeaProjects\\ScalaCassan\\src\\main\\scala\\MyNew.json");
val json_content = scala.io.Source.fromFile(input_file)
// parsing the json file
val details: Option[Any] = JSON.parseFull(json_content.mkString)
details match {
case mayBeList: Some[Any] =>
mayBeList.getOrElse(Seq.empty[Map[String, Any]]).asInstanceOf[List[Map[String, Any]]].map(_.values.toList).toSet
case None => println("Parsing failed")
}
in your match block :
- In first case I don't get why you are using
.tails.toSet.flatten
onAny
data type. - You can remove the third case as
Some
andNone
are the only possible outcomes ofOption
data-type.
answered Dec 18, 2018 at 13:17
6 Comments
Puneeth Reddy V
Updated the answer.
Vishal Reddy
details match { case mayBeList: Some[Map[String, Any]] => val z = mayBeList.get.tails.toList.flatten z.map(.values).foreach(x => println(x.toList.mkString(", "))) case None => println("Parsing failed") case other => println("Unknown data structure: " + other) } throwing the following error..: Error:(26, 14) value values is not a member of (String, Any) z.map(.values).foreach(x => println(x.toList.mkString(", ")))
Puneeth Reddy V
the above mentioned solution is not type safe. It will work only for data in the format of
[{"Name" : "Harish","Company" : "In Equity","Sal" : "50000"}, {"Name" : "Veer","Company" : "InOut","Sal" : "20000"}]
. Can you also show what is the JSON structure you are providing?Puneeth Reddy V
Also can you tell me why are you using
mayBeList.get.tails.toSet.flatten
on Any
data type?Vishal Reddy
json:::: [{ "Name": "Harish", "Company": "In Equity", "Sal": "50000" }, { "Name": "Chandra", "Company": "InSome", "Sal": "60000" }, { "Name": "Singh", "Company": "InWay", "Sal": "30000" }, { "Name": "Veer", "Company": "InOut", "Sal": "20000" }, { "Name": "Zara", "Company": "InWhich", "Sal": "90000" }] i am using "toSet.flatten" because it gives me uniqueu results where as if i use "toList" then it is giving me duplicate records.
|
scala> val l = List(Map("Name" -> "Harish", "Company" -> "In Equity", "Sal" -> 50000),
| Map("Name" -> "Veer", "Company" -> "InOut", "Sal" -> 20000),
| Map("Name" -> "Zara", "Company" -> "InWhich", "Sal" -> 90000),
| Map("Name" -> "Singh", "Company" -> "InWay", "Sal" -> 30000),
| Map("Name" -> "Chandra", "Company" -> "InSome", "Sal" -> 60000)
| )
l: List[scala.collection.immutable.Map[String,Any]] = List(Map(Name -> Harish, Company -> In Equity, Sal -> 50000), Map(Name -> Veer, Company -> InOut, Sal -> 20000), Map(Name -> Zara, Company -> InWhich, Sal -> 90000), Map(Name -> Singh, Company -> InWay, Sal -> 30000), Map(Name -> Chandra, Company -> InSome, Sal -> 60000))
scala> l.map(_.values).foreach(x => println(x.toList.mkString(", ")))
Harish, In Equity, 50000
Veer, InOut, 20000
Zara, InWhich, 90000
Singh, InWay, 30000
Chandra, InSome, 60000
answered Dec 18, 2018 at 14:41
1 Comment
Vishal Reddy
Hi airudah, thanks for your reply, but here you are hardcoding key and value pair in List(Map)) but the program i have written is reading json file dynamically, for which i need individual record values.
lang-scala
Map
has.values
method