Skip to content

Parse string values as JSON

Source code

Description

Throws an error if invalid JSON strings are encountered.

Usage

series_str_json_decode(dtype = NULL, ..., infer_schema_length = 100L)

Arguments

dtype The dtype to cast the extracted value to, or NULL (default). If NULL, the dtype will be inferred from the JSON value.
These dots are for future extensions and must be empty.
infer_schema_length The maximum number of rows to scan for schema inference. If set to NULL, the full data may be scanned (this is slow). Only used if the dtype argument is NULL.

Value

A polars Series

Examples

library("polars")

s1 <- as_polars_series(c('{"a":1, "b": true}', NA, '{"a":2, "b": false}'))

s2 <- s1$str$json_decode()
s2
#> shape: (3, 1)
#> ┌───────────┐
#> │           │
#> │ ---       │
#> │ struct[2] │
#> ╞═══════════╡
#> │ {1,true}  │
#> │ null      │
#> │ {2,false} │
#> └───────────┘
s2$dtype
#> Struct(`a`=Int64, `b`=Boolean)
s3 <- s1$str$json_decode(pl$Struct(a = pl$UInt8, b = pl$Boolean))
s3
#> shape: (3, 1)
#> ┌───────────┐
#> │           │
#> │ ---       │
#> │ struct[2] │
#> ╞═══════════╡
#> │ {1,true}  │
#> │ null      │
#> │ {2,false} │
#> └───────────┘
s3$dtype
#> Struct(`a`=UInt8, `b`=Boolean)