ukbaz
ukbaz

Reputation: 547

Extracting Values from a Map[String, Any] where Any is a Map itself

I have a Map which contains another Map in its value field. Here is an example of some records ;

(8702168053422489,Map(sequence -> 5, id -> 8702168053422489, type -> List(AppExperience, Session), time -> 527780267713))
(8702170626376335,Map(trackingInfo -> Map(trackId -> 14183197, location -> Browse, listId -> 3393626f-98e3-4973-8d38-6b2fb17454b5_27331247X28X6839X1506087469573, videoId -> 80161702, rank -> 0, row -> 1, imageKey -> boxshot|AD_e01f4a50-7e2b-11e7-a327-12789459b73e|en, requestId -> 662d92c2-6a1c-41a6-8ac4-bf2ae9f1ce68-417037), id -> 8702170626376335, sequence -> 59, time -> 527780275219, type -> List(NavigationLevel, Session), view -> details))
(8702168347359313,Map(muting -> false, id -> 8702168347359313, level -> 1, type -> List(Volume)))
(8702168321522401,Map(utcOffset -> 3600, type -> List(TimeZone), id -> 8702168321522401))
(8702171157207449,Map(trackingInfo -> Map(trackId -> 14183197, location -> Browse, listId -> 3393626f-98e3-4973-8d38-6b2fb17454b5_27331247X28X6839X1506087469573, videoId -> 80161356, rank -> 0, row -> 1, imageKey -> boxshot|AD_e01f4a50-7e2b-11e7-a327-12789459b73e|en, requestId -> 662d92c2-6a1c-41a6-8ac4-bf2ae9f1ce68-417037), id -> 8702171157207449, sequence -> 72, startOffset -> 0, time -> 527780278061, type -> List(StartPlay, Action, Session)))

The actual records I've interested in are the ones that contain trackingInfo, records 2 and 5.

What I would like to do is extract those and then extract some of the keys from there such as trackId. Something like this;

val trackingInfo = json("trackingInfo").asInstanceOf[Map[String, Any]]
val videoId = trackingInfo("videoId").asInstanceOf[Int]
val id = json("id").asInstanceOf[Long]
val sequence = json("sequence").asInstanceOf[Int]
val time = json("time").asInstanceOf[Long]
val eventType = json.get("type").getOrElse(List("")).asInstanceOf[List[String]]

To the extract the inner map, I've tired; myMap.map {case (k,v: collection.Map[_,_]) => v.toMap case _ => }

Which brings back the inner map but as a scala.collection.immutable.Iterable[Any] which leaves me in a puzzle on extracting values from it.

Any help is appreciated

Upvotes: 1

Views: 1082

Answers (1)

Denis Iakunchikov
Denis Iakunchikov

Reputation: 1349

Let's say you have a real map (I cut it a little bit)

val data: Map[ BigInt, Any ] = Map(
    BigInt( 8702168053422489L ) -> Map("sequence" -> "5", "id" -> BigInt( 8702168053422489L ) ),
    BigInt( 8702170626376335L ) -> Map("trackingInfo" -> Map("trackId" -> BigInt( 14183197 ), "location" -> "Browse" ), "id" -> BigInt( 8702170626376335L ) ),
    BigInt( 8702168347359313L ) -> Map("id" -> BigInt( 8702168347359313L ) ),
    BigInt( 8702168321522401L ) -> Map("id" -> BigInt( 8702168321522401L ) ),
    BigInt( 8702171157207449L ) -> Map("trackingInfo" -> Map("trackId" -> BigInt( 14183197 ), "location" -> "Browse" ), "id" -> BigInt( 8702171157207449L ) )
)

And you want to get records which have a trackingInfo key

val onlyWithTracking = data.filter( ( row ) => { 

    val recordToFilter = row._2 match {
        case trackRecord: Map[ String, Any ] => trackRecord
        case _ => Map( "trackId" -> Map() )
    }

    recordToFilter.contains( "trackingInfo" ) 
} )

And then process those records in some way

onlyWithTracking.foreach( ( row ) => {

    val record = row._2 match {
        case trackRecord: Map[ String, Any ] => trackRecord
        case _ => Map( "trackingInfo" -> Map() )
    }

    val trackingInfo = record( "trackingInfo" ) match {
        case trackRow: Map[ String, Any ] => trackRow
        case _ => Map( "trackId" -> "error" )
    }

    val trackId = trackingInfo( "trackId" )

    println( trackId )
} )

With this pattern matching I'm trying to ensure that using keys like trackingInfo or trackId is somewhat safe. You should implement more strict approach.

Upvotes: 1

Related Questions