Skip to content

Parse string values as JSON.

Description

Parse string values as JSON.

Usage

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

Arguments

dtype The dtype to cast the extracted value to. If NULL, the dtype will be inferred from the JSON value.
… These dots are for future extensions and must be empty.
infer_schema_length How many rows to parse to determine the schema. If NULL, all rows are used.

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}')
)

df$select(
  pl$col("json_val")$str$json_decode()
)$unnest("json_val")
#> shape: (3, 2)
#> ┌──────┬───────┐
#> │ a    ┆ b     │
#> │ ---  ┆ ---   │
#> │ i64  ┆ bool  │
#> ╞══════╪═══════╡
#> │ 1    ┆ true  │
#> │ null ┆ null  │
#> │ 2    ┆ 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 │
#> └──────┴───────┘