Forum Discussion
ToddChitt
2 years agoSuper User
Notebook to read complex JSON array
Hello all. I am trying to learn PySpark from this website:
https://sparkbyexamples.com/pyspark/pyspark-alias-column-examples/
Good info, but I am stuck. I borrowed the simple JSON code that lo...
- 2 years ago
Hi ToddChitt,
Wouldn't it be possible to use a couple of SQL functions like explode and col for this?
I found that suggested approach in this blog: https://medium.com/towards-data-engineering/transforming-json-to-lakehouse-tables-with-microsoft-fabric-notebooks-a-step-by-step-guide-d14458b1c284
Below is an example based on your json code in one of my test notebooks.
# Apply transformation to the dataframe from pyspark.sql.functions import col, explode exploded_df = df.select(explode(col("data")).alias("data")) tf_df = exploded_df.select( col("data.RecordNumber").alias("RecordNumber"), col("data.Zipcode").alias("Zipcode") ) display(tf_df) dfJSON1 = tf_df.select( col("RecordNumber"), col("Zipcode")) dfJSON1.show()
ToddChitt
2 years agoSuper User
Expiscornovus Thanks for the quick response.
Your sample code worked great. Now it's up to me to figure out how to shred the multi-level nested arrays in my actual JSON documents.
I will check out that blog and try to learn a little more about PySpark.
Thanks
- Anonymous2 years agoNot applicable
Glad to know you got some insights over your query. Please continue using Fabric Community on your further queries.