Return results of a sql query as JSON in oracle 12c

后端 未结 9 1606
误落风尘
误落风尘 2020-11-29 05:42

Background

I need to fetch a few thousands rows from Oracle and convert them to JSON for use in SlickGrid. Currently I am fetching the rows in PHP,

相关标签:
9条回答
  • 2020-11-29 06:38

    Starting Oracle 19c, the syntax to construct a JSON representation for a row of a table is simplified

    For Eg: To convert all the rows of the hr.employees to separate jsons, use

    SELECT JSON_OBJECT(*) FROM hr.employees ;
    
    {
    "EMPLOYEE_ID" : 100,
    "FIRST_NAME" : "Steven",
    "LAST_NAME" : "King",
    "EMAIL" : "SKING",
    "PHONE_NUMBER" : "515.123.4567",
    "HIRE_DATE" : "2003-06-17T00:00:00",
    "JOB_ID" : "AD_PRES",
    "SALARY" : 24000,
    "COMMISSION_PCT" : null,
    "MANAGER_ID" : null,
    "DEPARTMENT_ID" : 90
    }                       --row 1
    {
    "EMPLOYEE_ID" : 101,
    "FIRST_NAME" : "Neena",
    "LAST_NAME" : "Kochhar",
    "EMAIL" : "NKOCHHAR",
    "PHONE_NUMBER" : "515.123.4568",
    "HIRE_DATE" : "2005-09-21T00:00:00",
    "JOB_ID" : "AD_VP",
    "SALARY" : 17000,
    "COMMISSION_PCT" : null,
    "MANAGER_ID" : 100,
    "DEPARTMENT_ID" : 90
    }                       --row 2
     ...
    

    LIVE SQL example

    0 讨论(0)
  • 2020-11-29 06:42

    To add to the answer in oracle 12.2 , you can create json as you want like this .

    SELECT JSON_ARRAY(
    JSON_OBJECT (
             KEY 'number' VALUE s.number,
             KEY 'name' VALUE s.sname,
             KEY 'location' VALUE s.loc
              )
           ) AS student_det
    FROM   student s;
    
    0 讨论(0)
  • 2020-11-29 06:43

    I do not see Python solution (in case you need to dump JSON).

    I wrote json-ora-extract for medium size extracts (because data-set has to fit available memory).

    It uses wx_Oracle and json Python modules to read data from Oracle data base (any version) and dump it into *.json file.

    There's also an option to create compressed *.gz file.

    0 讨论(0)
提交回复
热议问题