pyspark.sql.functions.json_array_length#

pyspark.sql.functions.json_array_length(col)[source]#

Returns the number of elements in the outermost JSON array. NULL is returned in case of any other valid JSON string, NULL or an invalid JSON.

Added in version 3.5.0.

Parameters:
col: :class:`~pyspark.sql.Column` or str

target column to compute on. A column that evaluates to a string.

Returns:
Column

length of json array. Returns a column that evaluates to an integer.

Examples

>>> df = spark.createDataFrame([(None,), ('[1, 2, 3]',), ('[]',)], ['data'])
>>> df.select(json_array_length(df.data).alias('r')).collect()
[Row(r=None), Row(r=3), Row(r=0)]