How to convert Complex JSON string in to MAP in scala - java

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.

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
}

I got another solution which I think More easier.
I Made an own schema for the JSON and Used from_json method with the schema,and it worked well.
from_json(col("Players"),ownschema).as("new_Json")
and my ownschema contains the structure of the Json String.
For any doubts, Comment.
Happy Coding.

Related

Using JsonPath to parse JSON String recursively

I'm trying to parse a given JSON in String format, for example:
{
"id": "indeed",
"interaction_data":
"{\"data\":\"{\\\"something\\\":\\\"blabla\\\"}\",\"somethingElseNotNested\":\"Indeed\"}"
}
I'm working with Kotlin, and I called JsonPath.parse on the value above, the problem is, interaction_data is parsed as a String, instead of it being treated as a JSON as well.
So when I call read("$.interaction_data.data.something") it gives me an error, since interaction_data is treated as a String, instead of an object.
Any way around this? (other than parsing this part separately, I need to handle this generically).
Thanks!
Json interaction_data property is triple stringifyied. Why you don't try this
var jsonObject=..your json;
var jsonParsed=JSON.parse(jsonObject.interaction_data);
jsonParsed.data=JSON.parse(jsonParsed.data);
JsonObject.interaction_data=jsonParsed;
result
{
"id":"indeed",
"interaction_data":{"data"{"something":"blabla"},"somethingElseNotNested":"Indeed"}
}

Process json with different data type in Circe

In my case, there might be different data type of same json field. Example:
"need_exp":1500
or
"need_exp":"-"
How to process this case? I know it can be processed by parse or use custom encoders, but this is a very complex json text, is there any way to solve that without rewriting the whole decoder (for example, just "tell" the decoder to convert all Int to String in need_exp field)?
It is called a disjunction which can be encoded with the Scala standard Either class.
Simply map the json that to the following class:
case class Foo(need_exp: Either[String, Int])
My solution is to use a custom decoder. Rewrite a little part of the JSON can be fine.
For example, there is a simple JSON:
{
/*many fields*/
"hotList":[/* ... many lists inside*/],
"list":[ {/*... many fields*/
"level_info":{
"current_exp":11463,
"current_level":5,
"current_min":10800,
"next_exp":28800 //there is the problem
},
"sex":"\u4fdd\u5bc6"},/*...many lists*/]
}
In this case, I don't need to rewrite the whole JSON encoder, just write a custom encoder of level_info:
implicit val decodeUserLevel: Decoder[UserLevel] = (c: HCursor) => for
{
current_exp <- c.downField("current_exp").as[Int]
current_level <- c.downField("current_level").as[Int]
current_min <- c.downField("current_min").as[Int]
next_exp <- c.downField("next_exp").withFocus(_.mapString
{
case """-""" => "-1"
case default => default
}).as[Int]
} yield
{
UserLevel(current_exp, current_level, current_min, next_exp)
}
and it worked.

How to write my own JSON parser?

I want to write a JSON parser that parses input JSON of depth n for college assignment.
As far as I have understood, I will have to convert this JSON into <String, Object> map and then create classes out of it.
Is this correct? also How would I come to know exact datatypes of the values in JSON?
for ex my sample JSON sis.
{
“name”: “user”,
“address”: {
"city":"abc",
"zip":12345
}
}
Then I am supposed to create a class named say User that has to fields
1. name: String 2. Adderss : Object
and Address class having city : string and zip :int with getters and setters.
Is this correct? How to dynamically create a class?
How should I start ?
see this for that (not recommended): Creating classes dynamically with Java.
Advice of #T.J.Crowder is a good one: some collection.
public class json_java{
Map<String,Object> values=new HashMap<String,Object>;
To get the type:
JSONObject one_object=...
Object one_value = one_object.get("city");
one_value.getClass().getName(); // => String

use Scala to get JSON data in lower levels

I'm a beginner of Scala, and I have JSON data formatted like below:
{
"index_key": {
"time":"12938473",
"event_detail": {
"event_name":"click",
"location":"US"
}
}
}
I'm trying to get the content of "index_key" and extract the content of second level as a new JSON objet and initiate a class based on the second level data.
{
"time":"12938473",
"event_detail": {
"event_name":"click",
"location":"US"
}
}
I tried to use json4s to extract from the above json to be a Event class, but how to get rid of the "index_key" which is the first level key?
case class Detail(event_name: String, location: String)
case class Event(time: String, event_detail: Detail)
json.extract[Event]
I've read json4s documentation, and also http://www.scala-lang.org/api/2.10.3/index.html#scala.util.parsing.json.JSON$, but still don't quite get it, as it seems the pre-defined json should be fit for the parser?
Could anyone please tell me how to get the second level data (or any lower level) of the json structure?
You can use \ to get to the object you want to extract:
val json = parse(str) \ "index_key"
json.extract[Event]

Detect type of JSON attribute

JSON:
{"attribute1":11, "attribute2":"string atribute"}
I want to detect what kind of type are attribute1 and attribute2:
attribute1 is integer
attribute2 is string
jsonObject.getAttributeType("attribute2"); // should output: string/integer/boolean.
It was very easy to achieve in PHP or OBJC. Suggestions?
(I'm assuming that the Android for the org.json package is that same as you can find on the json.org site ... here.)
The only method on a JSONObject that will give you the underlying value ... without coercing it ... is JSONObject.get(name). If name is known, the result is the object that represents the value internally. I haven't done a comprehensive trawl of the code, but I think it can only be one of the following types:
Boolean, Long, Double, String, JSONArray, JSONObject
You should be able to discriminate these using instanceof.
But should be asking yourself if this is the right thing to do. The normal way to deal with JSON object attributes via the JSONObject API is to use the methods that coerce them into the type that you expect. In most cases, it doesn't matter if a number is sent as 42 or 42.0 or "42" ... and it is best not to be picky if the intent is easy to determine.
Another solution you can use the jackson library to do this,
import com.fasterxml.jackson.databind.JsonNode;
import com.github.fge.jackson.JsonLoader;
//Defining the JSON object
JSON json = {"attribute1":11, "attribute2":"string atribute"};
//Get the needed attribute
String value = json.get("attribute1");
//Convert the attribute to JsonNode
JsonNode value = JsonLoader.fromString(value);
//Then you can check type as below
value.isObject();
value.isArray();
value.isDouble();
value.isTextual();
value.isInt()

Categories

Resources