简体   繁体   English

以 JSON 格式返回 Oracle 查询结果

[英]Returning Oracle query results in JSON format

I want to create a python script to run an oracle query and store each resulting row into a JSON file.我想创建一个 python 脚本来运行 oracle 查询并将每个结果行存储到 JSON 文件中。 So far, I have completed the oracle connection, the query execution and the printing of each resulting row.到目前为止,我已经完成了 oracle 连接、查询执行和每个结果行的打印。

Note: I'm using cx_Oracle注意:我正在使用cx_Oracle

import cx_Oracle

try:
    con = cx_Oracle.connect('username/pass@127.0.0.1/oracle')
except cx_Oracle.DatabaseError as er:
    print('There is an error in the Oracle database:', er)

else:
    try:
        cur = con.cursor()

        # fetchall() is used to fetch all records from result set
        cur.execute('select * from products')
        rows = cur.fetchall()
        print(rows)


    except cx_Oracle.DatabaseError as er:
        print('There is an error in the Oracle database:', er)

    except Exception as er:
        print('Error:'+str(er))

    finally:
        if cur:
            cur.close()
finally:
    if con:
        con.close()

The following is the desired JSON output:以下是所需的 JSON output:

product_json product_json

{
    "arrayOfProducts": [
        {
            "id": 1,
            "name": "CHECK PRINT SHIRT",
            "price": 110
        },
        {
            "id": 2,
            "name": "GLORIA HIGH LOGO SNEAKER",
            "price": 91
        },
        {
            "id": 3,
            "name": "CATE RIGID BAG",
            "price": 94.5
        },
        {
            "id": 4,
            "name": "GUESS CONNECT WATCH",
            "price": 438.9
        }
    ]
}

Instead of using select * from products , you can get the JSON from the database using:您可以使用以下方法从数据库中获取 JSON,而不是使用select * from products

SELECT JSON_OBJECT(
         KEY 'arrayOfProducts'
         VALUE JSON_ARRAYAGG(
           JSON_OBJECT(
             KEY 'id'    VALUE id,
             KEY 'name'  VALUE name,
             KEY 'price' VALUE price
             -- RETURNING CLOB PRETTY
           )
           -- RETURNING CLOB PRETTY
         )
         -- RETURNING CLOB PRETTY
       ) AS json
FROM   products

(Note: uncomment the RETURNING CLOB PRETTY lines if you want it formatted with white space.) (注意:如果您希望它使用空格格式化,请取消注释RETURNING CLOB PRETTY行。)

Which, for the sample data:其中,对于样本数据:

CREATE TABLE products (id, name, price) AS
SELECT 1, 'name1', 110   FROM DUAL UNION ALL
SELECT 2, 'name2',  91   FROM DUAL UNION ALL
SELECT 3, 'name3',  94.5 FROM DUAL UNION ALL
SELECT 4, 'name4', 438.9 FROM DUAL;

Outputs:输出:

JSON JSON
{"arrayOfProducts":[{"id":1,"name":"name1","price":110},{"id":2,"name":"name2","price":91},{"id":3,"name":"name3","price":94.5},{"id":4,"name":"name4","price":438.9}]} {"arrayOfProducts":[{"id":1,"name":"name1","price":110},{"id":2,"name":"name2","price":91}, {"id":3,"name":"name3","price":94.5},{"id":4,"name":"name4","price":438.9}]}

db<>fiddle here db<> 在这里摆弄

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM