Home >Backend Development >Python Tutorial >Unable to decompose nested JSON in Spark dataframe
I am new to spark. I'm trying to flatten the dataframe but am not able to do it via "explode".
The original data frame structure is as follows:
id|approvaljson 1|[{"approvertype":"1st line manager","status":"approved"},{"approvertype":"2nd line manager","status":"approved"}] 2|[{"approvertype":"1st line manager","status":"approved"},{"approvertype":"2nd line manager","status":"rejected"}]
Do I need to convert it to the following schema?
id|approvaltype|status 1|1st line manager|approved 1|2nd line manager|approved 2|1st line manager|approved 2|2nd line manager|rejected
I've tried it
df_exploded = df.withcolumn("approvaljson", explode("approvaljson"))
But I got the error:
Cannot resolve "explode(ApprovalJSON)" due to data type mismatch: parameter 1 requires ("ARRAY" or "MAP") type, however, "ApprovalJSON" is of "STRING" type.;
First parse the json-like string into an array of structures, then use inline
to break the array into rows and columns
df1 = df.withcolumn("approvaljson", f.from_json("approvaljson", schema="array<struct<approvertype string, status string>>")) df1 = df1.select("id", f.inline('approvaljson'))
result
df1.show() +---+----------------+--------+ | ID| ApproverType| Status| +---+----------------+--------+ | 1|1st Line Manager|Approved| | 1|2nd Line Manager|Approved| | 2|1st Line Manager|Approved| | 2|2nd Line Manager|Rejected| +---+----------------+--------+
The above is the detailed content of Unable to decompose nested JSON in Spark dataframe. For more information, please follow other related articles on the PHP Chinese website!