0
// 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
asked Dec 18, 2018 at 12:58
3
  • Can you provide the content of the file you're using as input? Commented Dec 18, 2018 at 13:12
  • Map has .values method Commented Dec 18, 2018 at 13:16
  • [{ "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" }] Commented Dec 18, 2018 at 13:28

3 Answers 3

1

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

Obviously you can convert these to a List or Seq or whatever with the relevant .to... method (eg .toList, toSeq, etc).
Hi James thanks for your reply, but its throwing an error for me.
0

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 on Any data type.
  • You can remove the third case as Some and None are the only possible outcomes of Option data-type.
answered Dec 18, 2018 at 13:17

6 Comments

Updated the answer.
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(", ")))
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?
Also can you tell me why are you using mayBeList.get.tails.toSet.flatten on Any data type?
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.
|
0
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

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.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.