polars.Expr.str.json_path_match#

Expr.str.json_path_match(json_path: IntoExprColumn) Expr[source]#

Extract the first match from a JSON string using the provided JSONPath.

Throws errors if invalid JSON strings are encountered. All return values are cast to String, regardless of the original value.

Documentation on the JSONPath standard can be found here.

Parameters:
json_path

A valid JSONPath query string.

Returns:
Expr

Expression of data type String. Contains null values if original value is null or the json_path returns nothing.

Examples

>>> df = pl.DataFrame(
...     {"json_val": ['{"a":"1"}', None, '{"a":2}', '{"a":2.1}', '{"a":true}']}
... )
>>> df.with_columns(matched=pl.col("json_val").str.json_path_match("$.a"))
shape: (5, 2)
┌────────────┬─────────┐
│ json_val   ┆ matched │
│ ---        ┆ ---     │
│ str        ┆ str     │
╞════════════╪═════════╡
│ {"a":"1"}  ┆ 1       │
│ null       ┆ null    │
│ {"a":2}    ┆ 2       │
│ {"a":2.1}  ┆ 2.1     │
│ {"a":true} ┆ true    │
└────────────┴─────────┘