Uploaded image for project: 'Spark'
  1. Spark
  2. SPARK-29610

Keys with Null values are discarded when using to_json function

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Major
    • Resolution: Duplicate
    • 2.4.4
    • None
    • Build
    • None

    Description

      When calling to_json on a Struct if a key has Null as a value then the key is thrown away.

      import pyspark
      import pyspark.sql.functions as F
      
      l = [("a", "foo"), ("b", None)]
      
      df = spark.createDataFrame(l, ["id", "data"])
      (
        df.select(F.struct("*").alias("payload"))
          .withColumn("payload", 
            F.to_json(F.col("payload"))
          ).select("payload")
          .show()
      )

      Produces the following output:

      +--------------------+
      |             payload|
      +--------------------+
      |{"id":"a","data":...|
      |          {"id":"b"}|
      +--------------------+

      The `data` key in the second row has just been silently deleted.

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              Hourany Jonathan
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: