json_object_keys
Returns all the keys of the outermost JSON object as an array. If a valid JSON object is given, all the keys of the outermost object will be returned as an array. If it is any other valid JSON string, an invalid JSON string or an empty string, the function returns null.
Syntax
Python
from pyspark.sql import functions as sf
sf.json_object_keys(col)
Parameters
Parameter | Type | Description |
|---|---|---|
|
| Target column to compute on. |
Returns
pyspark.sql.Column: all the keys of the outermost JSON object.
Examples
Python
from pyspark.sql import functions as sf
df = spark.createDataFrame([(None,), ('{}',), ('{"key1":1, "key2":2}',)], ['data'])
df.select(sf.json_object_keys(df.data).alias('r')).collect()
Output
[Row(r=None), Row(r=[]), Row(r=['key1', 'key2'])]