0

我们存储在表中的 JSON 字符串具有以下格式:

[
   {
      "itemid" : "18726539",
      "duration" : "19:28",
      "title" : "Flight Plan for Trading: Market Lessons from My Pilot Dad",
      "description" : "<p>Learning to fly an airplane can build an excellent structured thought process for navigating markets</p>",
      "pubDate" : "Wed, 14 Apr 2021 18:04:19 +0000"
   }
]

当我使用 IS JSON 对其进行测试时,所有数据都会返回。因此,Oracle 将其视为 JSON。但是,我不确定如何获取“itemid”和“description”项目。我试过这个查询,但它是空的:

SELECT JSON_QUERY(json_str, '$.itemid' RETURNING  VARCHAR2(30)  WITH ARRAY WRAPPER) AS overview FROM json_podcast_data;

我在想前导括号可能会引起问题?

任何帮助是极大的赞赏。

4

2 回答 2

1

JSON_VALUE您可以使用单个函数,而不是使用多个函数JSON_TABLE

SELECT j.itemid,
       j.duration,
       j.title,
       j.description,
       TO_TIMESTAMP_TZ(
         j.pubdate,
         'Dy, DD Mon YYYY HH24:MI:SS TZHTZM',
         'NLS_DATE_LANGUAGE=American'
       ) AS pubdate
FROM   json_podcast_data d
       CROSS JOIN JSON_TABLE(
         d.json_str,
         '$[*]'
         ERROR ON ERROR
         COLUMNS(
           itemid      VARCHAR2(20)   PATH '$.itemid',
           duration    VARCHAR2(10)   PATH '$.duration',
           title       VARCHAR2(200)  PATH '$.title',
           description VARCHAR2(4000) PATH '$.description',
           pubdate     VARCHAR2(50)   PATH '$.pubDate'
         )
       ) j

其中,对于样本数据:

CREATE TABLE json_podcast_data (json_str CLOB CHECK (json_str IS JSON ) );

INSERT INTO json_podcast_data ( json_str ) VALUES (
'[
   {
      "itemid" : "18726539",
      "duration" : "19:28",
      "title" : "Flight Plan for Trading: Market Lessons from My Pilot Dad",
      "description" : "<p>Learning to fly an airplane can build an excellent structured thought process for navigating markets</p>",
      "pubDate" : "Wed, 14 Apr 2021 18:04:19 +0000"
   }
]' );

输出:

项目ID 期间 标题 描述 发布日期
18726539 19:28 交易飞行计划:我的飞行员爸爸的市场经验

学习驾驶飞机可以为导航市场建立一个优秀的结构化思维过程

2021-04-14 18:04:19.000000 +00:00
于 2021-07-27T21:57:28.387 回答
0

您可以使用获取 itemid 和描述的值JSON_VALUE。请参阅下面的示例

WITH
    json_podcast_data (json_str)
    AS
        (SELECT '[
   {
      "itemid" : "18726539",
      "duration" : "19:28",
      "title" : "Flight Plan for Trading: Market Lessons from My Pilot Dad",
      "description" : "<p>Learning to fly an airplane can build an excellent structured thought process for navigating markets</p>",
      "pubDate" : "Wed, 14 Apr 2021 18:04:19 +0000"
   }
]'
           FROM DUAL)
SELECT json_value (json_str, '$[0].itemid')          AS itemid,
       json_value (json_str, '$[0].description')     AS description
  FROM json_podcast_data;


     ITEMID                                                                                                    DESCRIPTION
___________ ______________________________________________________________________________________________________________
18726539    <p>Learning to fly an airplane can build an excellent structured thought process for navigating markets</p>

如果您的 JSON 数组中有多个项目,您可以使用该JSON_TABLE函数列出数组中的所有项目。

/* Formatted on 27-Jul-2021 5:51:38 PM (QP5 v5.354) */
WITH
    json_podcast_data (json_str)
    AS
        (SELECT '[
   {
      "itemid" : "18726539",
      "duration" : "19:28",
      "title" : "Flight Plan for Trading: Market Lessons from My Pilot Dad",
      "description" : "<p>Learning to fly an airplane can build an excellent structured thought process for navigating markets</p>",
      "pubDate" : "Wed, 14 Apr 2021 18:04:19 +0000"
   },
   {
      "itemid" : "23456",
      "duration" : "10:15",
      "title" : "Test Title",
      "description" : "<p>Some cool podcast</p>",
      "pubDate" : "Wed, 14 Apr 2021 18:04:19 +0000"
   }
]'
           FROM DUAL)
SELECT j.itemid, j.description
  FROM json_podcast_data
       CROSS JOIN
       JSON_TABLE (
           json_str,
           '$[*]'
           COLUMNS itemid NUMBER PATH '$.itemid', description VARCHAR2 PATH '$.description') j;


     ITEMID                                                                                                    DESCRIPTION
___________ ______________________________________________________________________________________________________________
   18726539 <p>Learning to fly an airplane can build an excellent structured thought process for navigating markets</p>
      23456 <p>Some cool podcast</p>
于 2021-07-27T21:47:09.043 回答