0

I hv a json file like below

{

"Item Version": 1.0,
"Action2": "true12",
"Item Creation Time": "2019-04-14 14:15:09",
"Trade Dictionary": {
    "Country": "India",
    "TradeNumber": "1",
    "action": {
        "Action2": true,
        "Action1": false
    },
    "Value": "XXXXXXXXXXXXXXX"
},
"Payments": {
    "abc": "def",
    "Payment Details": [{
        "Payment Date": "2019-04-11",
        "Payment Type": [{
            "Payval": "myval "
        }]
    }]
}

}

I have a scala code like this

def secondFlatten(map: Map[String, Any]):Map[String, Any]={val c=map.flatten {

val c=map.flatten {

 case ((key, map : Map[String, Any])) => {
   map.flatten {
     case ((innerKey: String, l: List[Map[String, Any]])) => l.head.flatten
     {

               case ((key1: String, l1: List[Map[String, Any]]))=>l1.head.map{case (x:String, y: Any)=> (s"$key->$innerKey->$key1->$x"-> y)}
               case ((key1: String, m1 : Map[String, Any])) =>  m1.map{case (x:String, y: Any)=> (s"$key->$innerKey->$key1->$x"-> y)}
               case (key1: String, value1: String) => Map(s"$key->$innerKey->$key1"-> value1)
     }
     case ((innerKey: String, m : Map[String, Any])) => m.map{case (x:String, y: Any)=> (s"$key->$innerKey->$x"-> y)}
     case (innerKey: String, value: String) => Map(s"$key->$innerKey"-> value)
}
}
case ((key, value)) => Map(key -> value)

}.toMap
return c

}

which returns values like this

(Payments->Payment Details->Payment Date,2019-04-11)

(Trade Dictionary->Value,XXXXXXXXXXXXXXX)

(Payments->Payment Details->Payment Type->Payval,myval )

(Trade Dictionary->Country,India)

(Trade Dictionary->TradeNumber,1)

(Trade Dictionary->action->Action1,false)

(Action2,true12)

(Item Version,1.0)

(Trade Dictionary->action->Action2,true)

(Item Creation Time,2019-04-14 14:15:09)

(Payments->abc,def)

Is there any way to write a recursive function instead of putting the same kind of case statement???

1 Answer 1

0

Here you go:

def flattenMap(m: Map[String, Any], tree: List[String] = List()) : Iterable[(String, Any)] = m.flatten {
  case (k: String, v: Map[String, Any] @unchecked) => flattenMap(v, tree :+ k)
  case (k: String, v: List[Map[String, Any]] @unchecked) => v.flatten(flattenMap(_, tree :+ k))
  case (k: String, v: Any) => List((tree :+ k.toString).mkString("->") -> v)
  case (k: String, null) => List((tree :+ k).mkString("->") -> null)
}

(repl here: https://scastie.scala-lang.org/benrobby/9fWtDyvyTt28BEul6UJ4Bg)

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

2 Comments

this does not work if value is null or any number..Like if the key value pair is "CLS": true, "money":2.378 "userComments": null, for this cases we get match error
just add another case statement, updated my answer.

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.