polars.Expr.str.json_decode#
- Expr.str.json_decode( ) Expr[source]#
Parse string values as JSON.
Throws an error if invalid JSON strings are encountered.
- Parameters:
- dtype
The dtype to cast the extracted value to.
- infer_schema_length
Deprecated and ignored.
Changed in version 1.33.0: Deprecate
infer_schema_lengthand makedtypenon-optional to ensure that the planner can determine the output datatype.
See also
json_path_matchExtract the first match from a JSON string using the provided JSONPath.
Examples
>>> df = pl.DataFrame( ... {"json": ['{"a":1, "b": true}', None, '{"a":2, "b": false}']} ... ) >>> dtype = pl.Struct([pl.Field("a", pl.Int64), pl.Field("b", pl.Boolean)]) >>> df.with_columns(decoded=pl.col("json").str.json_decode(dtype)) shape: (3, 2) ┌─────────────────────┬───────────┐ │ json ┆ decoded │ │ --- ┆ --- │ │ str ┆ struct[2] │ ╞═════════════════════╪═══════════╡ │ {"a":1, "b": true} ┆ {1,true} │ │ null ┆ null │ │ {"a":2, "b": false} ┆ {2,false} │ └─────────────────────┴───────────┘