pyspark.sql.functions.try_parse_json#

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

Parses a column containing a JSON string into a VariantType. Returns None if a string contains an invalid JSON value.

New in version 4.0.0.

Parameters
colColumn or str

a column or column name JSON formatted strings

Returns
Column

a new column of VariantType.

Examples

>>> df = spark.createDataFrame([ {'json': '''{ "a" : 1 }'''}, {'json': '''{a : 1}'''} ])
>>> df.select(to_json(try_parse_json(df.json))).collect()
[Row(to_json(try_parse_json(json))='{"a":1}'), Row(to_json(try_parse_json(json))=None)]