How to convert Complex JSON string in to MAP in scala

ADARSH K :

I have a text file which contains a line like

players={"Messi":{"Details":{"Goals":500},"Country":"Argentina"},"Neymar":{"Clubs":["Santos", "FC barcelona", "Paris saint German"], "Country":"Brazil"}}

Now I am used a regex for extract the

{"Messi":{"Details":{"Goals":500},"Country":"Argentina"},"Neymar":{"Clubs":["Santos", "FC barcelona", "Paris saint German"],"Country":"Brazil"}}

from the text file and pass it in to a case class which accepts the value as a String.

and I am making a Dataframe using this case class.

In my case every line may be different in the contents with in the JSON String.So I am looking for a general solution to Convert any complex Json string to Map values.

When checking dataframe.printSchema, I am getting the players column as a String type. But I need it to be as a Map type which holds a Key and value as a Struct type. I tried method referred in this link

How can I convert a json string to a scala map?

when I used this way,I got error

"org.json4s.package$MappingException: Do not know how to convert JObject(List((Details,JObject(List((Goals,JString(500))))), (Country,JString(Argentina)))) into class java.lang.String "

and I used following solutions

Converting JSON string to a JSON object in Scala

But these too won't worked for me.

This is my case class

case class caseClass (
                       Players :String = ""
                     )

I am Extracting the json string using a user defined function.

Simply my requirement is that I have a complex Json String which contains keys and values as struct,list etc..

so I want to make the string to its corresponding JSON which holds a proper schema with respect to its contents.

Kindly expecting Valuable solutions.

pme :

If you also can live with JsValue as value instead of String it looks a bit simpler:

import play.api.libs.json._

case class CaseClass (
           Players :Option[JsValue]
         )

object CaseClass {

  implicit val jsonFormat = Json.format[CaseClass ]
}

I saw some problems with your Json - so you would need to have something like:

val json = Json.parse("""{
  "Players":{
     "Messi":{"Details":{"Goals":500},"Country":"Argentina"},
     "Neymar":{"Clubs":["Santos", "FC barcelona", "Paris saint German"], "Country":"Brazil"}
  }
}"""
  )

To get a String out of this you can use:

  json.validate[CaseClass] match {
    case JsSuccess(cc, _) => cc.Players.toString
    case JsError(errors) => // handle errors
  }  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=69601&siteId=1
Recommended