Skip to content

Convert this struct to a string column with json values

Description

Convert this struct to a string column with json values

Usage

<Expr>$struct$json_encode()

Value

A polars expression

Examples

library("polars")

df <- pl$DataFrame(
  a = list(1:2, c(9, 1, 3)),
  b = list(45, NA)
)$select(a = pl$struct("a", "b"))

df
#> shape: (2, 1)
#> ┌──────────────────────────┐
#> │ a                        │
#> │ ---                      │
#> │ struct[2]                │
#> ╞══════════════════════════╡
#> │ {[1.0, 2.0],[45.0]}      │
#> │ {[9.0, 1.0, 3.0],[null]} │
#> └──────────────────────────┘
df$with_columns(encoded = pl$col("a")$struct$json_encode())
#> shape: (2, 2)
#> ┌──────────────────────────┬────────────────────────────────┐
#> │ a                        ┆ encoded                        │
#> │ ---                      ┆ ---                            │
#> │ struct[2]                ┆ str                            │
#> ╞══════════════════════════╪════════════════════════════════╡
#> │ {[1.0, 2.0],[45.0]}      ┆ {"a":[1.0,2.0],"b":[45.0]}     │
#> │ {[9.0, 1.0, 3.0],[null]} ┆ {"a":[9.0,1.0,3.0],"b":[null]} │
#> └──────────────────────────┴────────────────────────────────┘