variant_get
Extracts a sub-variant from v according to path, and then cast the sub-variant to targetType. Returns null if the path does not exist. Throws an exception if the cast fails.
Syntax
Python
from pyspark.sql import functions as sf
sf.variant_get(v, path, targetType)
Parameters
Parameter | Type | Description |
|---|---|---|
|
| A variant column or column name. |
|
| A column containing the extraction path strings or a string representing the extraction path. A valid path should start with |
| str | The target data type to cast into, in a DDL-formatted string. |
Returns
pyspark.sql.Column: a column of targetType representing the extracted result
Examples
Python
from pyspark.sql import functions as sf
df = spark.createDataFrame([ {'json': '''{ "a" : 1 }''', 'path': '$.a'} ])
v = sf.parse_json(df.json)
df.select(sf.variant_get(v, "$.a", "int").alias("r")).collect()
Output
[Row(r=1)]
Python
df.select(sf.variant_get(v, "$.b", "int").alias("r")).collect()
Output
[Row(r=None)]
Python
df.select(sf.variant_get(sf.parse_json(df.json), df.path, "int").alias("r")).collect()
Output
[Row(r=1)]