polars.internals.expr.string.ExprStringNameSpace.json_path_match#

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

Extract the first match of json string with provided JSONPath expression.

Throw errors if encounter invalid json strings. All return value will be casted to Utf8 regardless of the original value.

Documentation on JSONPath standard can be found here.

Parameters:
json_path

A valid JSON path query string.

Returns:
Utf8 array. Contain null if original value is null or the json_path return
nothing.

Examples

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