This question already has an answer here:
I have a json file that looks like this
{
"group" : {},
"lang" : [
[ 1, "scala", "functional" ],
[ 2, "java","object" ],
[ 3, "py","interpreted" ]
]
}
I tried to create a dataframe using
val path = "some/path/to/jsonFile.json"
val df = sqlContext.read.json(path)
df.show()
when I run this I get
df: org.apache.spark.sql.DataFrame = [_corrupt_record: string]
How do we create a df based on contents of "lang" key? I do not care about group{} all I need is, pull data out of "lang" and apply case class like this
case class ProgLang (id: Int, lang: String, type: String )
I have read this post Reading JSON with Apache Spark - `corrupt_record` and understand that each record needs to be on a newline but in my case I cannot change the file structure
The
json
format is wrong. The thejson
api ofsqlContext
is reading it as corrupt record. Correct form isand supposing you have it in a file ("/home/test.json"), then you can use following method to get the
dataframe
you wantYou should have
Updated
If you don't want to change your input json format as mentioned in your comment below, you can use
wholeTextFiles
to read thejson
file andparse
it as belowIt should give you
dataframe
as above andschema
asAs of Spark 2.2 you can use
multiLine
option to deal with the case of multi-line JSONs.Before Spark 2.2 see How to access sub-entities in JSON file? or Read multiline JSON in Apache Spark.