JSON_ EACH
Represents the SQLite json_each() table-valued function.
This function allows you to iterate over the top-level elements of a JSON array or the top-level key-value pairs of a JSON object. For each element or pair, it returns a single row with columns describing the key, value, type, etc.
It provides a "shallow" iteration, meaning it does not recurse into nested objects or arrays. To iterate recursively through an entire JSON structure, use JSON_TREE.
Example
val json = JSON_EACH(JsonLiteral("""{"name": "Alice", "age": 30}"""))
select(json.key, json.value)
.from(json)
.execute()
.forEach {
println(it[json.key])
println(it[json.value])
}Example with table
object Users : KQLiteTable("user") {
val name = stringColumn("name")
val phone = jsonColumn("phone") // multiple phone numbers
}
val jsonEach = JSON_EACH(Users.phone)
select(Users.name)
.from(Users, jsonEach)
.where {
JSON_VALID(Users.phone).EQ(true) AND jsonEach.value.LIKE("123%")
}.execute()
.forEach {
println(it.getString(0))
}Parameters
The column containing the JSON string to iterate over. This can be any column type, as it will be treated as text.
An optional JSON path to a specific object or array within the JSON string. If provided, json_each will iterate over that nested element instead of the root.
See also
for a description of the output columns.
for recursive iteration.