visibility 954 comment 1 access_time 11 months ago languageEnglish
S Surendiran
articleArticles 0
codeCode 0
imageDiagrams 0
descriptionNotebooks 0
chat_bubble_outlineThreads 1
commentComments 0
loyaltyKontext Points 5

how to prevent backslash while writing json file in pyspark

I need to write a data into json file like in the below format using pyspark.

"list-item": [ {
{"author":"author1","title":"title1","pages":1,"email":""}, {
{"author":"author2","title":"title2","pages":2,"email":""}, {
{"author":"author3","title":"title3","pages":3,"email":""}, {
{"author":"author4","title":"title4","pages":4,"email":""}, ],

"version": 1

I have written the below pyspark code but it write "" and adding "" at the beginning and end of each item. How to remove the backslash and double quote

import sys
from pyspark.sql import SparkSession
from pyspark.sql.types import *
from pyspark.sql.functions import col,to_json,struct,collect_list,lit
from datetime import datetime
from time import time
if __name__ == '__main__': spark = SparkSession.builder.appName(
spark = SparkSession.builder.appName("Test").enableHiveSupport().getOrCreate() schema = StructType([ StructField(
schema = StructType([
StructField("author", StringType(), False), StructField(
StructField("title", StringType(), False), StructField(
StructField("pages", IntegerType(), False), StructField(
StructField("email", StringType(), False) ]) data = [ [
data = [
["author1", "title1", 1, ""], [
["author2", "title2", 2, ""], [
["author3", "title3", 3, ""], [
["author4", "title4", 4, ""] ] df = spark.createDataFrame(data, schema)
df = spark.createDataFrame(data, schema)"author", "title", "pages", "email")).alias("json-data")).agg(collect_list("json-data").alias("list-item")) df=df.withColumn(
df=df.withColumn("version",lit("1.0").cast(IntegerType())) df.printSchema()
df.printSchema(), False) curDT = targetPath = curDT.strftime("%m-%d-%Y-%H-%M-%S") df.write. df.write.format("json").mode("overwrite").option("escape", "").save(targetPath)

my code writes the json with backslash and double quote enclosed each item like to remove those.Please help

info Last modified by Administrator 11 months ago
thumb_up 0

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts

comment Comments
11 months ago link more_vert
Kontext Kontext
articleArticles 80
codeCode 45
imageDiagrams 20
descriptionNotebooks 0
chat_bubble_outlineThreads 1
commentComments 7
loyaltyKontext Points 1137
#1665 Re: how to prevent backslash while writing json file in pyspark

It behaves in that way because you have converted the object to JSON string. 

Try the following code to remove to_json and let the final DataFrameWriter to convert it as JSON format properly:"author", "title", "pages", "email").alias("json-data")).agg(collect_list("json-data").alias("list-item"))

The code will generate the following output:


recommendMore from Kontext