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: 

parsing json string value column into dataframe structure

dipali_globant
New Contributor II

Hi All,

I have to read kafka payload which has value column with json string. But the format of the json is as below.

{ "data": [ { "p_al4": "N/A", "p_a5": "N/A", "p_ad": "OA003", "p_aName": "Abc", "p_aFlag": true ,....(dynamic)} ] }

In data key it can have dynamic number of key value pairs.

here "p_" is a column and respective value is given.
Please suggest how to create this payload into dataframe structure.
like below:

+-----+-----+-----+-------+------+------------------+
|p_al4 | p_a5 | p_ad | p_aName | p_aFlag|
+-----+-----+-----+-------+------+----------------+
|N/A | N/A | OU13| Admin | true |
+-----+-----+-----+-------+------+--------------

2 REPLIES 2

SparkJun
Databricks Employee
Databricks Employee

Would you know the element in the Kafka event JSON ahead of the time or count the number of element each time and then generate the schema for such and pass it from_json to parse?  https://spark.apache.org/docs/latest/api/python/reference/pyspark.sql/api/pyspark.sql.functions.from...

dipali_globant
New Contributor II

No I don't know element in JSON . so I can't define structure.

Join Us as a Local Community Builder!

Passionate about hosting events and connecting people? Help us grow a vibrant local community—sign up today to get started!

Sign Up Now