JSON_REPLACE

fun <T : KQLiteJson> JSON_REPLACE(json: KQLiteColumn<T>, vararg pathValue: Pair<String, Any?>): KQLiteColumn<T>

Replaces existing values in a JSON document at specified paths.

This function returns a new JSON document that is a copy of the input json with existing values replaced at the given paths. It only replaces values in locations that already exist. If a specified path does not have a value, the new value is ignored.

Use JSON_INSERT to add new values without overwriting existing ones, and JSON_SET to either update existing values or insert new ones.

Example:

val jsonCol = jsonColumn("data") // Contains '{"a":2,"c":[4,5]}'

// Replace the value at key 'a' and the first element of array 'c'
// SELECT JSON_REPLACE(data, '$.a', 9, '$.c[0]', 99)
// Result: '{"a":9,"c":[99,5]}'
select(JSON_REPLACE(jsonCol, "$.a" to 9, "$.c[0]" to 99)).from(MyTable).execute()

// Attempt to replace at a non-existent path 'b' (will be ignored)
// SELECT JSON_REPLACE(data, '$.b', 7)
// Result: '{"a":2,"c":[4,5]}'
select(JSON_REPLACE(jsonCol, "$.b" to 7)).from(MyTable).execute()

Return

A KQLiteColumn of given type json containing the resulting JSON document.

Author

MOHAMMAD AZIM ANSARI

Parameters

json

The KQLiteJson column to modify.

pathValue

Pairs of JSONPath expressions and the new values to set at those paths.

See also