cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Explode function to flatten the JSON

David_Billa
New Contributor III

I've the DDL as below.

 

Create or replace table test 
(
prices ARRAY<STRUCT<Ord:STRING:,Vndr:STRING,Prc:STRING>>
)
using delta
location "path"

 

Now I want to flatten the JSON and I've tried as below but it's throwing an error, "[UNRESOLVED.COLUMN.WITH_SUGGESTION]'

 

select explode(output.prices) as prices
from table

 

If I use 'select prices.ord from table' then I see the value as ["5"] with square brackets. I don't want to see the square brackets in the result.

Any help?

1 ACCEPTED SOLUTION

Accepted Solutions

Walter_C
Databricks Employee
Databricks Employee

Do you get the same result if you remove the output.?

View solution in original post

2 REPLIES 2

Walter_C
Databricks Employee
Databricks Employee

Do you get the same result if you remove the output.?

hari-prasad
Valued Contributor II

Hi @David_Billa ,

You can use following from_json function in spark which can convert struct into individual column. Refer this link https://spark.apache.org/docs/3.4.0/api/python/reference/pyspark.sql/api/pyspark.sql.functions.from_....

Also, you can use dot notation to access individual column from struct, like df.withColumn('Ord', col('prices.Ord')).

 

Regards,
Hari Prasad



Regards,
Hari Prasad

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you wonโ€™t want to miss the chance to attend and share knowledge.

If there isnโ€™t a group near you, start one and help create a community that brings people together.

Request a New Group