0

I have this array

val myJson = {
"record": {
"recordId": 100,
"name": "xyz",
"version": "1.1",
"input": [
  {
    "format": "Database",
    "type": "Oracle",
    "connectionStringId": "212",
    "connectionString": "ksfksfklsdflk",
    "schemaName": "schema1",
    "databaseName": "db1",
    "tables": [
      {
        "table_name":"one"
      }
      {
        "table_name":"two"
      }
    ]
  }
]    
}
}

I am using this code to get this json in dataframe

val df = sparkSession.read.json(myjson)

I want values of schemaName & databaseName, how can i get them?

val schemaName = df.select("record.input.schemaName") //not working

Someone, please help me

2
  • You can't read json this way, you have to load it from file or create dataset. val df = spark.read.json(Seq(myJson).toDS()). Your "input" is an array so you should pass required index, for example df.select("record.input[0].schemaName") Commented Feb 11, 2021 at 10:03
  • I am reading it from file only, sorry df.select("record.input[0].schemaName") --> this i already tried, doesn't work Commented Feb 11, 2021 at 10:05

1 Answer 1

1

You need to explode the array column record.input then select the fields you want :

df.select(explode(col("record.input")).as("inputs"))
  .select("inputs.schemaName", "inputs.databaseName")
  .show

//+----------+------------+
//|schemaName|databaseName|
//+----------+------------+
//|   schema1|         db1|
//+----------+------------+
Sign up to request clarification or add additional context in comments.

Comments

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.