Home > Backend Development > Python Tutorial > Unable to decompose nested JSON in Spark dataframe

Unable to decompose nested JSON in Spark dataframe

WBOY
Release: 2024-02-11 10:51:03
forward
465 people have browsed it

无法分解 Spark 数据框中的嵌套 JSON

Question content

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"}]
Copy after login

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
Copy after login

I've tried it

df_exploded = df.withcolumn("approvaljson", explode("approvaljson"))
Copy after login

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.;
Copy after login


Correct answer


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'))
Copy after login

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|
+---+----------------+--------+
Copy after login

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!

Related labels:
source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template