CREATE TABLE demo_plus.dbo.wm_json_demo (
id int IDENTITY(1,1) NOT NULL,
json_detail varchar(max) NOT NULL,
CONSTRAINT PK_wm_json_demo PRIMARY KEY (id)
下面以wm_json_demo 为例,演示以上5个json操作相关的函数
INSERT INTO demo_plus.dbo.wm_json_demo (json_detail) VALUES('{"key":3,"value1":10,"value2":[{"vv21":13,"vv22":103}],"value3":null}');
INSERT INTO demo_plus.dbo.wm_json_demo (json_detail) VALUES('{"key":4,"value1":15,"value2":[{"vv21":13,"vv22":103}],"value3":"10"}');
INSERT INTO demo_plus.dbo.wm_json_demo (json_detail) VALUES('{"key":7,"value1":20,"value2":[{"vv21":13,"vv22":103}],"value3":"15"}');
IsJson:判断一个字符串是不是合法的Json格式。是返回1,否返回0,null返回null。
SELECT
IsJson(json_detail) as IsJson
demo_plus.dbo.wm_json_demo
Json_Value:从Json字段中提取值
用法:Json_Value (cloumn_name,’$.json_field_name’) from table
Json_Query:Json字符串中提取对象或数组。
用法:Json_Query (cloumn_name,’$.json_field_name’) from table
SELECT
JSON_VALUE(json_detail,'$.key') as 'key',
JSON_VALUE(json_detail,'$.value1') as value1,
JSON_value(json_detail,'$.value2') as value2,
JSON_QUERY(json_detail,'$.value2') as value2_query,
JSON_VALUE(json_detail,'$.value3') as value3
from demo_plus.dbo.wm_json_demo;