[英]How do I make an ORDS query return a JSON_OBJECT without quotes?
I am trying to build a JSON to return as the result of an HTTP GET using ORDS on Oracle Database XE 18c.我正在尝试构建一个 JSON,作为在 Oracle Database XE 18c 上使用 ORDS 的 HTTP GET 结果返回。 Using JSON_OBJECT and similar functions, the SELECT always seems to surround the resulting JSON with quotes and escape the quotes within the object.
使用 JSON_OBJECT 和类似的函数,SELECT 似乎总是用引号包围生成的 JSON,并在对象中转义引号。 Example:
例子:
CREATE TABLE test_cases (
team VARCHAR2(3),
response_time NUMBER
);
INSERT INTO test_cases (team, response_time) VALUES ('foo', 1);
INSERT INTO test_cases (team, response_time) VALUES ('foo', 2);
INSERT INTO test_cases (team, response_time) VALUES ('foo', 5);
INSERT INTO test_cases (team, response_time) VALUES ('bar', 5);
INSERT INTO test_cases (team, response_time) VALUES ('bar', 7);
INSERT INTO test_cases (team, response_time) VALUES ('bar', 9);
COMMIT;
BEGIN
ORDS.define_module(
p_module_name => 'rest',
p_base_path => 'rest/',
p_items_per_page => 0
);
ORDS.define_template(
p_module_name => 'rest',
p_pattern => 'stats/'
);
ORDS.define_handler(
p_module_name => 'rest',
p_pattern => 'stats/',
p_method => 'GET',
p_source_type => ORDS.source_type_query_one_row,
p_source => '
SELECT JSON_OBJECTAGG (
KEY t.team VALUE AVG(t.response_time)
)
AS averages
FROM test_cases t
GROUP BY t.team
',
p_items_per_page => 0
);
COMMIT;
END;
/
Requesting the resource gives this result:请求资源会给出以下结果:
$ curl -i -H "Content-Type: application/json" -X GET "http://localhost:8080/ords/rest/stats/"
HTTP/1.1 200 OK
Content-Type: application/json
{"averages":"{\"foo\":2.66666666666666666666666666666666666667,\"bar\":7}"}
where the JSON value of "averages" is quoted.其中引用了“平均值”的 JSON 值。 The behavior seems to be particular to JSON_OBJECT, because other SELECT calls using the same ORDS parameters do not add quotes to the results.
该行为似乎是 JSON_OBJECT 特有的,因为使用相同 ORDS 参数的其他 SELECT 调用不会在结果中添加引号。
Is there a way to de-stringify the output of JSON_OBJECT before building it into the result of the SELECT?有没有办法在将 JSON_OBJECT 的输出构建到 SELECT 的结果中之前对其进行去字符串化?
Since you're generating the JSON yourself, you want to change your SOURCE_TYPE to Media Resource.由于您自己生成 JSON,因此您希望将 SOURCE_TYPE 更改为媒体资源。 Then in your query, the first column will be your mime type so your browser knows how to handle the incoming binary data.
然后在您的查询中,第一列将是您的 MIME 类型,以便您的浏览器知道如何处理传入的二进制数据。
Try this -尝试这个 -
BEGIN
ORDS.DEFINE_HANDLER(
p_module_name => 'rest',
p_pattern => 'stats/',
p_method => 'GET',
p_source_type => 'resource/lob',
p_items_per_page => 0,
p_mimes_allowed => '',
p_comments => NULL,
p_source =>
'SELECT ''application/json'', JSON_OBJECTAGG (
KEY t.team VALUE AVG(t.response_time)
)
AS averages
FROM test_cases t
GROUP BY t.team '
);
COMMIT;
END;
/
Then I make the GET call in my browser -然后我在浏览器中进行 GET 调用 -
{
"foo": 2.6666666666666665,
"bar": 7
}
Now, let's say you also have some regular data, but only ONE of the columns is JSON being stored in or generated by the DB - you want ORDS to JSON-ify your regular data, but not the data that is ALREADY json.现在,假设您还有一些常规数据,但只有一列是 JSON 存储在 DB 中或由 DB 生成 - 您希望 ORDS 对您的常规数据进行 JSON 化,但不是 ALREADY json 的数据。
There's a way to have your cake and eat it too, on a silver spoon.有一种方法可以在银勺上吃蛋糕并吃掉它。
BEGIN
ORDS.DEFINE_HANDLER(
p_module_name => 'rest',
p_pattern => 'stats/',
p_method => 'GET',
p_source_type => 'json/collection',
p_items_per_page => 0,
p_mimes_allowed => '',
p_comments => NULL,
p_source =>
'SELECT JSON_OBJECTAGG (
KEY t.team VALUE AVG(t.response_time)
)
"{}jsons"
FROM test_cases t
GROUP BY t.team '
);
COMMIT;
END;
/
For your JSON data, add this column alias对于您的 JSON 数据,添加此列别名
"{}jsons"
Running this now looks like so -现在运行它看起来像这样 -
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.