为提高效率,提问时请提供以下信息,问题描述清晰可优先响应。 【DM版本】:7 【操作系统】:win11 【CPU】: 【问题描述】*:如何获取json字符串中的值
这里给个例子:具体可以在 达梦SQL语言使用手册中查看JSON用法介绍。 CREATE TABLE j_purchaseorder (id int NOT NULL, date_loaded TIMESTAMP (6) WITH TIME ZONE, po_document VARCHAR CONSTRAINT ensure_json CHECK (po_document IS JSON));
INSERT INTO j_purchaseorder VALUES (1,SYSTIMESTAMP,'{ “PONumber” : 1600, “Reference” : “ABULL-20140421”, “Requestor” : “Alexis Bull”, “User” : “ABULL”, “CostCenter” : “A50”, “ShippingInstructions” : {“name” : “Alexis Bull”, “Address”: {“street” : “200 Sporting Green”, “city” : “South San Francisco”, “state” : “CA”, “zipCode” : 99236, “country” : “United States of America”}, “Phone” : [{“type” : “Office”, “number” : “909-555-7307”}, {“type” : “Mobile”, “number” : “415-555-1234”}] }, “Special Instructions” : null, “AllowPartialShipment” : true, “LineItems” : [{“ItemNumber” : 1, “Part” : {“Description” : “One Magic Christmas”, “UnitPrice” : 19.95, “UPCCode” : 13131092899}, “Quantity” : 9.0}, {“ItemNumber” : 2, “Part” : {“Description” : “Lethal Weapon”, “UnitPrice” : 19.95, “UPCCode” : 85391628927}, “Quantity” : 5.0}] } ');
/* 查询所有字符串 */ select * from j_purchaseorder;
/* 查询指定对象值。 json_value 函数的返回值必须是单值且是标量数据类型。 */ SELECT ID,json_value(po_document, ‘$.Requestor’) FROM j_purchaseorder;
/* 查询指定对象数组值。 json_query 的返回结果是一个或多个 JSON 数据。多值返回时必须指定 WITH WRAPPER。单值返回时,标量类型必须指定 WITH WRAPPER,object 或 array 则不需要。 */ select json_query(po_document,‘$.ShippingInstructions.Phone’) FROM j_purchaseorder;
/* 查询指定对象数组中指定的对象值 */ SELECT ID,json_value(po_document, ‘$.ShippingInstructions.Phone[0].type’) FROM j_purchaseorder;
这里给个例子:具体可以在 达梦SQL语言使用手册中查看JSON用法介绍。
CREATE TABLE j_purchaseorder
(id int NOT NULL,
date_loaded TIMESTAMP (6) WITH TIME ZONE,
po_document VARCHAR
CONSTRAINT ensure_json CHECK (po_document IS JSON));
INSERT INTO j_purchaseorder VALUES (1,SYSTIMESTAMP,'{
“PONumber” : 1600,
“Reference” : “ABULL-20140421”,
“Requestor” : “Alexis Bull”,
“User” : “ABULL”,
“CostCenter” : “A50”,
“ShippingInstructions” : {“name” : “Alexis Bull”,
“Address”: {“street” : “200 Sporting Green”,
“city” : “South San Francisco”,
“state” : “CA”,
“zipCode” : 99236,
“country” : “United States of America”},
“Phone” : [{“type” : “Office”, “number” : “909-555-7307”},
{“type” : “Mobile”, “number” : “415-555-1234”}]
},
“Special Instructions” : null,
“AllowPartialShipment” : true,
“LineItems” : [{“ItemNumber” : 1,
“Part” : {“Description” : “One Magic Christmas”,
“UnitPrice” : 19.95,
“UPCCode” : 13131092899}, “Quantity” : 9.0},
{“ItemNumber” : 2,
“Part” : {“Description” : “Lethal Weapon”,
“UnitPrice” : 19.95,
“UPCCode” : 85391628927},
“Quantity” : 5.0}]
}
');
/*
查询所有字符串
*/
select * from j_purchaseorder;
/*
查询指定对象值。
json_value 函数的返回值必须是单值且是标量数据类型。
*/
SELECT ID,json_value(po_document, ‘$.Requestor’) FROM j_purchaseorder;
/*
查询指定对象数组值。
json_query 的返回结果是一个或多个 JSON 数据。多值返回时必须指定 WITH
WRAPPER。单值返回时,标量类型必须指定 WITH WRAPPER,object 或 array 则不需要。
*/
select json_query(po_document,‘$.ShippingInstructions.Phone’) FROM j_purchaseorder;
/*
查询指定对象数组中指定的对象值
*/
SELECT ID,json_value(po_document, ‘$.ShippingInstructions.Phone[0].type’) FROM j_purchaseorder;