mirror of
https://github.com/lancedb/lancedb.git
synced 2026-12-03 07:45:17 +00:00
The JSON function reference promises index acceleration for every accessor. Document the key and type requirements for typed lookups, explain serialized ordering for `json_extract`, and link to `EXPLAIN` so readers can check their plan. Checked the examples against LanceDB's pinned Lance implementation and verified the documentation link and `git diff --check`. Related to lance-format/lance#8806.
61 lines
2.1 KiB
Plaintext
61 lines
2.1 KiB
Plaintext
---
|
|
title: "JSON functions"
|
|
sidebarTitle: "JSON Functions"
|
|
description: "json_get and its typed variants, json_extract, and the rest of Lance's JSON accessor functions."
|
|
icon: brackets-curly
|
|
keywords: ["json_get", "json_extract", "json", "sql", "scalar function"]
|
|
availability:
|
|
embedded: unavailable
|
|
enterprise: available
|
|
---
|
|
|
|
| Function | Returns |
|
|
| --- | --- |
|
|
| `json_get(col, key)` | the value at `key`, still JSON |
|
|
| `json_get_string(col, key)` | `Utf8` |
|
|
| `json_get_int(col, key)` | `Int64` |
|
|
| `json_get_float(col, key)` | `Float64` |
|
|
| `json_get_bool(col, key)` | `Boolean` |
|
|
| `json_extract(col, path)` | `Utf8`: the value as JSON text, by path |
|
|
| `json_extract_with_type(col, path)` | a struct of the value as JSON and a type tag, by path |
|
|
| `json_exists(col, path)` | `Boolean` |
|
|
| `json_array_contains(col, value)` | `Boolean` |
|
|
| `json_array_length(col)` | length |
|
|
|
|
`json_get` returns JSON, so a typed accessor can read a field inside it:
|
|
|
|
<CodeGroup>
|
|
```sql SQL icon="database"
|
|
SELECT * FROM events
|
|
WHERE json_get_string(json_get(data, 'user'), 'name') = 'Alice'
|
|
```
|
|
</CodeGroup>
|
|
|
|
That works one level down. A `json_get` nested inside another `json_get` is
|
|
refused, so reach a deeper value with `json_extract` and a JSONPath instead. It
|
|
returns JSON text, so a string compares with its quotes:
|
|
|
|
<CodeGroup>
|
|
```sql SQL icon="database"
|
|
SELECT * FROM events
|
|
WHERE json_extract(data, '$.user.profile.name') = '"Alice"'
|
|
```
|
|
</CodeGroup>
|
|
|
|
A JSON scalar index on the key `status`, trained on integers, can serve
|
|
`json_get_int(data, 'status') = 200`. The key and accessor type must match the
|
|
index. A comparison on `json_extract` uses the serialized text. For example,
|
|
`'10'` sorts before `'2'`, even when both values are JSON numbers. Use
|
|
[`EXPLAIN`](/sql/dql/explain) to check whether your filter uses a scalar index.
|
|
|
|
## What to read next {#next}
|
|
|
|
<CardGroup cols={2}>
|
|
<Card title="JSON data type" icon="brackets-curly" href="/sql/data-types#json">
|
|
How a JSON column is stored, and reading it as a typed value.
|
|
</Card>
|
|
<Card title="CREATE INDEX" icon="bolt" href="/sql/ddl/index">
|
|
Create a scalar index for filters.
|
|
</Card>
|
|
</CardGroup>
|