[英]how to Convert JSON String to JSON object in pyspark
I have one of column type of data frame is string but actually it is containing json object of 4 schema where few fields are common. 我有数据框的列类型之一是字符串,但实际上它包含4个架构的json对象,其中很少有字段是常见的。 I need to convert that into jason object.
我需要将其转换为杰森对象。
Here is schema of data frame : 这是数据框架的架构:
query.printSchema()
query.printSchema()
root
|-- test: string (nullable = true)
value of DF looks like DF的值看起来像
query.show(10)
query.show(10)
+--------------------+
| test|
+--------------------+
|{"PurchaseActivit...|
|{"PurchaseActivit...|
|{"PurchaseActivit...|
|{"Interaction":{"...|
|{"PurchaseActivit...|
|{"Interaction":{"...|
|{"PurchaseActivit...|
|{"PurchaseActivit...|
|{"PurchaseActivit...|
|{"PurchaseActivit...|
+--------------------+
only showing top 10 rows
What solution i applied :: 我应用了什么解决方案::
query.write.format("text").mode('overwrite').save("s3://bucketname/temp/")
query.write.format( “文本”).mode( '覆盖')保存( “S3:// bucketname /温度/”)。
df = spark.read.json("s3a://bucketname/temp/")
df = spark.read.json(“ s3a:// bucketname / temp /”)
df.printSchema()
df.printSchema()
root |-- EventDate: string (nullable = true) |-- EventId: string (nullable = true) |-- EventNotificationType: long (nullable = true) |-- Interaction: struct (nullable = true) | |-- ContextId: string (nullable = true) | |-- Created: string (nullable = true) | |-- Description: string (nullable = true) | |-- Id: string (nullable = true) | |-- ModelContextId: string (nullable = true) |-- PurchaseActivity: struct (nullable = true) | |-- BillingCity: string (nullable = true) | |-- BillingCountry: string (nullable = true) | |-- ShippingAndHandlingAmount: double (nullable = true) | |-- ShippingDiscountAmount: double (nullable = true) | |-- SubscriberId: long (nullable = true) | |-- SubscriptionOriginalEndDate: string (nullable = true) |-- SubscriptionChurn: struct (nullable = true) | |-- PaymentTypeCode: long (nullable = true) | |-- PaymentTypeName: string (nullable = true) | |-- PreviousPaidAmount: double (nullable = true) | |-- SubscriptionRemoved: string (nullable = true) | |-- SubscriptionStartDate: string (nullable = true) |-- TransactionDetail: struct (nullable = true) | |-- Amount: double (nullable = true) | |-- OrderShipToCountry: string (nullable = true) | |-- PayPalUserName: string (nullable = true) | |-- PaymentSubTypeCode: long (nullable = true) | |-- PaymentSubTypeName: string (nullable = true)
Is there any best way where i don't need to write dataframe as text file and read it again as json file to get expected output 有什么最好的方法,我不需要将dataframe作为文本文件写入并再次作为json文件读取以获得预期的输出
You can use from_json()
before you write into text file, but you need to define the schema first. 在写入文本文件之前,可以使用
from_json()
,但是需要首先定义架构。
the code look like this : 代码看起来像这样:
data = query.select(from_json("test",schema=schema).alias("value")).selectExpr("value.*")
data.write.format("text").mode('overwrite').save("s3://bucketname/temp/")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.