Skip to content

Parse string values as JSON.

Source code

Description

Parse string values as JSON.

Usage

<Expr>$str$json_decode(dtype, ..., infer_schema_length = deprecated())

Arguments

dtype The dtype to cast the extracted value to.
These dots are for future extensions and must be empty.
infer_schema_length [Deprecated] Ignored.

Details

Throw errors if encounter invalid json strings.

Value

A polars expression

Examples

library("polars")

df <- pl$DataFrame(
  json_val = c('{"a":1, "b": true}', NA, '{"a":2, "b": false}')
)

dtype <- pl$Struct(a = pl$UInt8, b = pl$Boolean)
df$select(
  pl$col("json_val")$str$json_decode(dtype)
)$unnest("json_val")
#> shape: (3, 2)
#> ┌──────┬───────┐
#> │ a    ┆ b     │
#> │ ---  ┆ ---   │
#> │ u8   ┆ bool  │
#> ╞══════╪═══════╡
#> │ 1    ┆ true  │
#> │ null ┆ null  │
#> │ 2    ┆ false │
#> └──────┴───────┘