2

I have the below JSON structure which I am trying to convert to a structure with each element as column as shown below using Spark SQL. Explode(control) is not working. Can someone please suggest a way to do this?

Input:

{
"control" : [
    {
      "controlIndex": 0,
      "customerValue": 100.0,
      "guid": "abcd",
      "defaultValue": 50.0,
      "type": "discrete"
    },
    {
      "controlIndex": 1,
      "customerValue": 50.0,
      "guid": "pqrs",
      "defaultValue": 50.0,
      "type": "discrete"
    }
  ]
}

Desired output:

controlIndex customerValue guid defaultValult  type

0            100.0         abcd   50.0         discrete

1            50.0          pqrs   50.0         discrete
2
  • 1
    Can you add the code you tried? Commented Oct 17, 2017 at 15:59
  • I tried this - select explode(control) from myview It makes one column and puts each structure inside that Commented Oct 17, 2017 at 17:26

3 Answers 3

3

Addition to Paul Leclercq's answer, here is what can work.

import org.apache.spark.sql.functions.explode   
df.select(explode($"control").as("control")).select("control.*")
Sign up to request clarification or add additional context in comments.

Comments

1

Explode will create a new row for each element in the given array or map column

import org.apache.spark.sql.functions.explode   

df.select(
  explode($"control")
)    

Comments

-1

Explode will not work here as its not a normal array column but an array of struct. You might want to try something like

df.select(col("control.controlIndex"), col("control.customerValue"), col ("control. guid"), col("control. defaultValue"), col(control. type))

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.