Share via


VariantVal class

A class to represent a Variant value in Python.

Syntax

from pyspark.sql.types import VariantVal

VariantVal(value, metadata)

Parameters

Parameter Type Description
value bytes The bytes representing the value component of the Variant.
metadata bytes The bytes representing the metadata component of the Variant.

Methods

Method Description
toPython() Convert the VariantVal to a Python data structure.
toJson() Convert the VariantVal to a JSON string. The zone ID represents the time zone that the timestamp should be printed in. It is defaulted to UTC. The list of valid zone IDs can be found by importing the zoneinfo module and running zoneinfo.available_timezones().
parseJson() Convert the VariantVal to a nested Python object of Python data types.

Examples

from pyspark.sql import functions as sf
df = spark.createDataFrame([ {'json': '''{ "a" : 1 }'''} ])
v = df.select(sf.parse_json(df.json).alias("var")).head().var
v.toPython()
# {'a': 1}
v.toJson()
# '{"a":1}'