如何使用Oracle SQL生成JSON

时间:2018-04-17 13:48:11

标签: sql json oracle

我正在尝试使用Oracle

获取JSON字符串

我的Oracle版本是12.1.0.2.0,由于无法升级,我无法使用JSON_ARRAYAGG

The Json I want is exactly same with the picture

2 个答案:

答案 0 :(得分:3)

基于客户端的解决方案,其中客户端为Oracle SQL Developer

查询:

SELECT /*json*/ ID, NAME CUST_NAME, ADDRESS,
 CURSOR(
  SELECT ACCOUNT_ID,
         NAME ACCOUNT_NAME,
         BALANCE
  FROM ACCOUNTS
  WHERE CUST.ID = CUSTOMER_ID) AS "accounts",
  '../customers/' || ID || '/pic' AS "$signature"
FROM customers cust

这个有趣的部分是/*json*/位。这告诉我们的脚本引擎将结果和格式转换为JSON。 It also supports csv, insert statements, xml, html, etc.

查询从我的表中返回3行,并返回一个帐户位,这是通过查询的CURSOR位每行的一些嵌套结果。

当通过F5执行(作为脚本执行)时,SQL Developer返回此JSON

{  
   "results":[  
      {  
         "columns":[  
            {  
               "name":"ID",
               "type":"NUMBER"
            },
            {  
               "name":"CUST_NAME",
               "type":"VARCHAR2"
            },
            {  
               "name":"ADDRESS",
               "type":"VARCHAR2"
            },
            {  
               "name":"accounts",
               "type":"REFCURSOR"
            },
            {  
               "name":"$signature",
               "type":"VARCHAR2"
            }
         ],
         "items":[  
            {  
               "id":1,
               "cust_name":"Jeff",
               "address":"101 Maple Ln",
               "accounts":[  
                  {  
                     "account_id":100,
                     "account_name":"College Fund",
                     "balance":25.99
                  },
                  {  
                     "account_id":101,
                     "account_name":"NewCar",
                     "balance":30000
                  }
               ],
               "$signature":"..\/customers\/1\/pic"
            },
            {  
               "id":2,
               "cust_name":"Kris",
               "address":"911 Pine Dr",
               "accounts":[  
                  {  
                     "account_id":200,
                     "account_name":"Checking",
                     "balance":42.25
                  },
                  {  
                     "account_id":201,
                     "account_name":"Savings",
                     "balance":64000
                  }
               ],
               "$signature":"..\/customers\/2\/pic"
            },
            {  
               "id":3,
               "cust_name":"Colm",
               "address":"404 Irish Corner",
               "accounts":[  
                  {  
                     "account_id":300,
                     "account_name":"Potatoes",
                     "balance":2500.75
                  },
                  {  
                     "account_id":301,
                     "account_name":"Speeding Tickets",
                     "balance":1900
                  }
               ],
               "$signature":"..\/customers\/3\/pic"
            }
         ]
      }
   ]
}

我们还有一个中间层解决方案,Oracle REST Data Services。这允许您使用GET处理程序创建RESTful服务以运行SQL或PL / SQL块,其中结果以JSON格式返回。

答案 1 :(得分:0)

Oracle中的JSON支持在12.2之前非常有限。 This blog has posts on 4 different ways在Oracle 12.1中生成JSON。

在普通PL / SQL中相对容易做到的两个是: