MariaDB JSON_OBJECT_FILTER_KEYS(): Filter an Object by Keys
MariaDB JSON_OBJECT_FILTER_KEYS() returns a new JSON object containing the key-value pairs whose keys appear in a supplied array of strings. The values come from the input object. It is available from MariaDB 11.2. See the official MariaDB JSON_OBJECT_FILTER_KEYS reference.
Syntax
JSON_OBJECT_FILTER_KEYS(json_object, key_array)
Example: keep keys shared by two objects
Find the keys that occur in both objects, then keep those key-value pairs from the first object:
SET @obj1 = '{"a": 1, "b": 2, "c": 3}';
SET @obj2 = '{"b": 10, "c": 20, "d": 30}';
SELECT JSON_OBJECT_FILTER_KEYS(
@obj1,
JSON_ARRAY_INTERSECT(JSON_KEYS(@obj1), JSON_KEYS(@obj2))
);
{"b": 2, "c": 3}The output keeps the values from @obj1 (2 and 3), even though the matching keys in @obj2 have different values. For a Boolean check that two documents share a key-value pair, use JSON_OVERLAPS().
Advertisement