Trying to parse JSON

@jimka, @crater2150, @sangamon, @curoli, and All, I read the above comments So with the hope asking help, please help me with this question, here you can see Input Json and expected Output json.

I have tried with this code to find the relation tree for each node, but this is not giving output for List of Maps. I want the relation tree for List of Maps too, and with incremental identity for each similar siblings node. Output example is below:

val jsonFile = “C:/Users/Meenu/Desktop/Test.json”
val jsonData = scala.io.Source.fromFile(jsonFile).mkString

def convertJsonToMap(jsonData: String): Map[String, Any] = {
  implicit val formats = org.json4s.DefaultFormats
  parse(jsonData).extract[Map[String, Any]]
}
val flattenJson = convertJsonToMap(jsonData)

def traverse(el: Any, acc: List[String] = List.empty[String]): Map[String, Any] = el match {
case leaf: Int => Map(acc.reverse.mkString(“~>”) → leaf)
case leaf: BigInt => Map(acc.reverse.mkString(“~>”) → leaf)
case leaf: Double => Map(acc.reverse.mkString(“~>”) → leaf)
case leaf: List[JString] => Map(acc.reverse.mkString(“~>”) → leaf)
case leaf: String => Map(acc.reverse.mkString(“~>”) → leaf)
case leaf: List[List[DefaultJsonFormats]] => Map(acc.reverse.mkString(“~>”) → leaf)
case leaf: List[JArray] => Map(acc.reverse.mkString(“~>”) → leaf)
case data: Map[String, Any] => data flatMap {
case (k, v) => traverse(v, k :: acc)
}
} println(traverse(flattenJson))

OUTPUT is : For DISK and partition I am not getting the relation tree, which I need with incremental Number… Please help.

Test~>Hardware~>ThrottlePct → 100, Test~>Hardware~>Disk → List(Map(Name → DISK0, Bytes_Write_MB → 694, HardwareID → 650FEC74, Write_Pct → 4, Idle_Pct → 91, Model → TOSHIBA MQ01, Bytes → 263, Size → 476937, DiskID → F91B1F36, Partition → List(Map(Name → Not Used, HardwareID → 650FEC74, Size_MB → 500, DiskGUID → F91B1F36, Index → 0))

I want like : Test~>Hardware~>ThrottlePct → 100, Test~>Hardware~>Disk~>Disk-1~>Partition~>Partion-1 // Because disk has multiple instance with multiple Partition in each Disk.

Finally, I have to split the Json for each children along with its relation tree. Looking for help :slight_smile: