2021-11-03 11:40:35 +00:00
|
|
|
-- { echo }
|
|
|
|
SELECT '--JSON_VALUE--';
|
|
|
|
--JSON_VALUE--
|
2023-03-12 09:03:17 +00:00
|
|
|
SELECT JSON_VALUE('{"hello":1}', '$');
|
|
|
|
{"hello":1}
|
2021-11-03 11:40:35 +00:00
|
|
|
SELECT JSON_VALUE('{"hello":1}', '$.hello');
|
|
|
|
1
|
|
|
|
SELECT JSON_VALUE('{"hello":1.2}', '$.hello');
|
|
|
|
1.2
|
|
|
|
SELECT JSON_VALUE('{"hello":true}', '$.hello');
|
|
|
|
true
|
|
|
|
SELECT JSON_VALUE('{"hello":"world"}', '$.hello');
|
|
|
|
world
|
|
|
|
SELECT JSON_VALUE('{"hello":null}', '$.hello');
|
|
|
|
null
|
|
|
|
SELECT JSON_VALUE('{"hello":["world","world2"]}', '$.hello');
|
2023-03-12 09:03:17 +00:00
|
|
|
["world","world2"]
|
2021-11-03 11:40:35 +00:00
|
|
|
SELECT JSON_VALUE('{"hello":{"world":"!"}}', '$.hello');
|
2023-03-12 09:03:17 +00:00
|
|
|
{"world":"!"}
|
|
|
|
SELECT JSON_VALUE('{hello:world}', '$.hello');
|
|
|
|
null
|
2021-11-03 11:40:35 +00:00
|
|
|
SELECT JSON_VALUE('', '$.hello');
|
2023-03-12 09:03:17 +00:00
|
|
|
null
|
2021-11-03 11:40:35 +00:00
|
|
|
SELECT JSON_VALUE('{"foo foo":"bar"}', '$."foo foo"');
|
|
|
|
bar
|
2021-11-04 14:11:01 +00:00
|
|
|
SELECT JSON_VALUE('{"hello":"\\uD83C\\uDF3A \\uD83C\\uDF38 \\uD83C\\uDF37 Hello, World \\uD83C\\uDF37 \\uD83C\\uDF38 \\uD83C\\uDF3A"}', '$.hello');
|
|
|
|
🌺 🌸 🌷 Hello, World 🌷 🌸 🌺
|
2021-11-08 10:00:42 +00:00
|
|
|
SELECT JSON_VALUE('{"a":"Hello \\"World\\" \\\\"}', '$.a');
|
|
|
|
Hello "World" \\
|
|
|
|
select JSON_VALUE('{"a":"\\n\\u0000"}', '$.a');
|
|
|
|
\n\0
|
|
|
|
select JSON_VALUE('{"a":"\\u263a"}', '$.a');
|
|
|
|
☺
|
2023-03-12 09:03:17 +00:00
|
|
|
select JSON_VALUE('{"a":"b"}', "$.b");
|
|
|
|
null
|
2021-11-03 11:40:35 +00:00
|
|
|
SELECT '--JSON_QUERY--';
|
|
|
|
--JSON_QUERY--
|
|
|
|
SELECT JSON_QUERY('{"hello":1}', '$');
|
|
|
|
[{"hello":1}]
|
|
|
|
SELECT JSON_QUERY('{"hello":1}', '$.hello');
|
|
|
|
[1]
|
|
|
|
SELECT JSON_QUERY('{"hello":1.2}', '$.hello');
|
|
|
|
[1.2]
|
|
|
|
SELECT JSON_QUERY('{"hello":true}', '$.hello');
|
|
|
|
[true]
|
|
|
|
SELECT JSON_QUERY('{"hello":"world"}', '$.hello');
|
|
|
|
["world"]
|
|
|
|
SELECT JSON_QUERY('{"hello":null}', '$.hello');
|
|
|
|
[null]
|
|
|
|
SELECT JSON_QUERY('{"hello":["world","world2"]}', '$.hello');
|
|
|
|
[["world","world2"]]
|
|
|
|
SELECT JSON_QUERY('{"hello":{"world":"!"}}', '$.hello');
|
|
|
|
[{"world":"!"}]
|
|
|
|
SELECT JSON_QUERY( '{hello:{"world":"!"}}}', '$.hello'); -- invalid json => default value (empty string)
|
|
|
|
|
|
|
|
SELECT JSON_QUERY('', '$.hello');
|
|
|
|
|
|
|
|
SELECT JSON_QUERY('{"array":[[0, 1, 2, 3, 4, 5], [0, -1, -2, -3, -4, -5]]}', '$.array[*][0 to 2, 4]');
|
|
|
|
[0, 1, 4, 0, -1, -4]
|
|
|
|
SELECT '--JSON_EXISTS--';
|
|
|
|
--JSON_EXISTS--
|
|
|
|
SELECT JSON_EXISTS('{"hello":1}', '$');
|
|
|
|
1
|
|
|
|
SELECT JSON_EXISTS('', '$');
|
|
|
|
0
|
|
|
|
SELECT JSON_EXISTS('{}', '$');
|
|
|
|
1
|
|
|
|
SELECT JSON_EXISTS('{"hello":1}', '$.hello');
|
|
|
|
1
|
|
|
|
SELECT JSON_EXISTS('{"hello":1,"world":2}', '$.world');
|
|
|
|
1
|
|
|
|
SELECT JSON_EXISTS('{"hello":{"world":1}}', '$.world');
|
|
|
|
0
|
|
|
|
SELECT JSON_EXISTS('{"hello":{"world":1}}', '$.hello.world');
|
|
|
|
1
|
|
|
|
SELECT JSON_EXISTS('{hello:world}', '$.hello'); -- invalid json => default value (zero integer)
|
|
|
|
0
|
|
|
|
SELECT JSON_EXISTS('', '$.hello');
|
|
|
|
0
|
|
|
|
SELECT JSON_EXISTS('{"hello":["world"]}', '$.hello[*]');
|
|
|
|
1
|
|
|
|
SELECT JSON_EXISTS('{"hello":["world"]}', '$.hello[0]');
|
|
|
|
1
|
|
|
|
SELECT JSON_EXISTS('{"hello":["world"]}', '$.hello[1]');
|
|
|
|
0
|
|
|
|
SELECT JSON_EXISTS('{"a":[{"b":1},{"c":2}]}', '$.a[*].b');
|
|
|
|
1
|
|
|
|
SELECT JSON_EXISTS('{"a":[{"b":1},{"c":2}]}', '$.a[*].f');
|
|
|
|
0
|
|
|
|
SELECT JSON_EXISTS('{"a":[[{"b":1}, {"g":1}],[{"h":1},{"y":1}]]}', '$.a[*][0].h');
|
|
|
|
1
|
|
|
|
SELECT '--MANY ROWS--';
|
|
|
|
--MANY ROWS--
|
|
|
|
DROP TABLE IF EXISTS 01889_sql_json;
|
|
|
|
CREATE TABLE 01889_sql_json (id UInt8, json String) ENGINE = MergeTree ORDER BY id;
|
|
|
|
INSERT INTO 01889_sql_json(id, json) VALUES(0, '{"name":"Ivan","surname":"Ivanov","friends":["Vasily","Kostya","Artyom"]}');
|
|
|
|
INSERT INTO 01889_sql_json(id, json) VALUES(1, '{"name":"Katya","surname":"Baltica","friends":["Tihon","Ernest","Innokentiy"]}');
|
|
|
|
INSERT INTO 01889_sql_json(id, json) VALUES(2, '{"name":"Vitali","surname":"Brown","friends":["Katya","Anatoliy","Ivan","Oleg"]}');
|
|
|
|
SELECT id, JSON_QUERY(json, '$.friends[0 to 2]') FROM 01889_sql_json ORDER BY id;
|
|
|
|
0 ["Vasily", "Kostya"]
|
|
|
|
1 ["Tihon", "Ernest"]
|
|
|
|
2 ["Katya", "Anatoliy"]
|
2021-11-18 08:45:59 +00:00
|
|
|
SELECT id, JSON_VALUE(json, '$.friends[0]') FROM 01889_sql_json ORDER BY id;
|
|
|
|
0 Vasily
|
|
|
|
1 Tihon
|
|
|
|
2 Katya
|
2021-11-03 11:40:35 +00:00
|
|
|
DROP TABLE 01889_sql_json;
|