Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEA] support json to struct function #7867

Closed
nvliyuan opened this issue Mar 9, 2023 · 0 comments · Fixed by #8174
Closed

[FEA] support json to struct function #7867

nvliyuan opened this issue Mar 9, 2023 · 0 comments · Fixed by #8174
Assignees
Labels
feature request New feature or request

Comments

@nvliyuan
Copy link
Collaborator

nvliyuan commented Mar 9, 2023

I wish we can support json to struct function:

import pyspark.sql.functions as fn
from pyspark.sql.types import *

input_data = [
  (0, """{"c1": "0", "c2": "ccc", "c3": "xxxx", "c4": "xx"}"""),
  (1, """{"c1": "0", "c2": "cccc", "c3": "xxxx", "c4": "xx"}""")
]

myDF = spark.createDataFrame(input_data, ["id", "myjson"])

myDF.write.parquet("file:/yourpath/testjson")
myDF2 = spark.read.parquet("file:/yourpath/testjson")

schema = StructType([
    StructField("c1", StringType(), True),
    StructField("c2", StringType(), True),
    StructField("c3", StringType(), True),
    StructField("c4", StringType(), True),
])

myDF2.select(fn.from_json(fn.col("myjson").cast("string"),schema)).show()

image

@nvliyuan nvliyuan added feature request New feature or request ? - Needs Triage Need team to review and classify labels Mar 9, 2023
@nvliyuan nvliyuan changed the title [FEA] support cast struct to string in from_json function [FEA] support json to struct function Mar 9, 2023
@mattahrens mattahrens removed the ? - Needs Triage Need team to review and classify label Mar 21, 2023
@cindyyuanjiang cindyyuanjiang linked a pull request Apr 23, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants