PR to create a Daft value from a JSON string! - r...
# daft-dev
r
PR to create a Daft value from a JSON string! • reviewer: @Cory Grinstead • customer request: @NikkTheGreek via https://github.com/Eventual-Inc/Daft/issues/3994 • PR: https://github.com/Eventual-Inc/Daft/pull/4438 Example
Copy code
import daft

df = daft.from_pydict({

    "person": [
        '{"name": "Alice", "age": 30}',
        '{"name": "Bob", "age": 25}',
        '{"name": "Charlie", "age": 35}',
    ]
})

# STRUCT<name: STRING, age: BIGINT>
person_type = dt.struct(
    {
        "name": dt.string(),
        "age": dt.int64(),
    }
)

df.select(df["person"].from_json(person_type)).show()

╭────────────────────────────────╮
│ person                         │
│ ---                            │
│ Struct[name: Utf8, age: Int64] │
╞════════════════════════════════╡
│ {name: Alice,                  │
│ age: 30,                       │
│ }                              │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ {name: Bob,                    │
│ age: 25,                       │
│ }                              │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ {name: Charlie,                │
│ age: 35,                       │
│ }                              │
╰────────────────────────────────╯

(Showing first 3 of 3 rows)
🙌 3
🙏 1
n
@Robert Howell awesome 🥳 Is a ddl schema also supported? Like "name string, age int"
r
It supports passing the SQL type string based on this reference! https://www.getdaft.io/projects/docs/en/stable/sql/datatypes/#daft.datatype.DataType
💯 1
@Cory Grinstead in an effort to keep moving, I scoped some parts out. Now we have
deserialize
which accepts a string input and format argument. Follow-ups are in the description. https://github.com/Eventual-Inc/Daft/pull/4438