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.

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