I den här artikeln ger jag ett exempel på att exportera data till JSON-fil i Oracle 11g med PL/SQL.
Följ dessa steg för att exportera data till JSON-fil i Oracle 11g
- Hämta först Alexandria PL/SQL-verktygspaketet från GITHUB med följande länk:Ladda ner alexandria_plsql_utility-paketet.
- När du har laddat ner zip-filen, extrahera den och hitta JSON_UTIL_PKG i katalogen "alexandria-plsql-utils-master\alexandria-plsql-utils-master\ora".
- Installera JSON_UTIL_PKG-paketspecifikation och brödtext i ditt schema
- Hämta sedan "WRITE_CLOB_TO_FILE" PL/SQL-proceduren från följande länk:Ladda ned Write_Clob_To_File.
- Installera även denna procedur i ditt schema.
Nu kan du generera JSON-filen från Oracle-tabellen med PL/SQL, som visas i exemplet nedan.
Oracle 11g SQL till JSON-exempel
I följande exempel kommer vi att hämta data till CLOB-variabeln med JSON_UTIL_PKG.SQL_TO_JSON-funktionen och sedan skriver vi att CLOB har JSON-data i en FIL med WRITE_CLOB_TO_FILE-proceduren . Vi skickar tre parametrar till WRITE_CLOB_TO_FILE-proceduren, och de är följande:ett filnamn, katalogobjektets namn och CLOB-variabeln.
DECLARE c CLOB; BEGIN SELECT json_util_pkg.sql_to_json('select EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO from emp2') INTO c FROM DUAL; WRITE_CLOB_TO_FILE ('emp.json', 'JSON_DIR', c); END; /
Nu kan du kontrollera platsen för katalogobjektet JSON_DIR, och du kommer att hitta JSON-filen som visas i exemplet nedan:
{"ROWSET":[{"EMPNO":7369,"ENAME":"SMITH","JOB":"CLERK","MGR":7902,"HIREDATE":"17-DEC-80","SAL":800,"COMM":null,"DEPTNO":21},{"EMPNO":7499,"ENAME":"ALLEN","JOB":"SALESMAN","MGR":7698,"HIREDATE":"20-FEB-81","SAL":1600,"COMM":300,"DEPTNO":30},{"EMPNO":7521,"ENAME":"WARD","JOB":"SALESMAN","MGR":7698,"HIREDATE":"22-FEB-81","SAL":1250,"COMM":500,"DEPTNO":30},{"EMPNO":7566,"ENAME":"JONES","JOB":"MANAGER","MGR":7839,"HIREDATE":"04-FEB-81","SAL":2975,"COMM":null,"DEPTNO":20},{"EMPNO":7654,"ENAME":"MARTIN","JOB":"SALESMAN","MGR":7698,"HIREDATE":"28-SEP-81","SAL":1250,"COMM":1400,"DEPTNO":30},{"EMPNO":7698,"ENAME":"BLAKE","JOB":"MANAGER","MGR":7839,"HIREDATE":"05-JAN-81","SAL":2850,"COMM":null,"DEPTNO":30},{"EMPNO":7782,"ENAME":"CLARK","JOB":"MANAGER","MGR":7839,"HIREDATE":"06-SEP-81","SAL":2450,"COMM":null,"DEPTNO":10},{"EMPNO":7788,"ENAME":"SCOTT","JOB":"ANALYST","MGR":7566,"HIREDATE":"19-APR-87","SAL":3000,"COMM":null,"DEPTNO":20},{"EMPNO":7839,"ENAME":"KING","JOB":"PRESIDENT","MGR":null,"HIREDATE":"17-NOV-81","SAL":5000,"COMM":null,"DEPTNO":10},{"EMPNO":7844,"ENAME":"TURNER","JOB":"SALESMAN","MGR":7698,"HIREDATE":"09-AUG-81","SAL":1500,"COMM":0,"DEPTNO":30},{"EMPNO":7876,"ENAME":"ADAMS","JOB":"CLERK","MGR":7788,"HIREDATE":"23-MAY-87","SAL":1100,"COMM":null,"DEPTNO":20},{"EMPNO":7900,"ENAME":"JAMES","JOB":"CLERK","MGR":7698,"HIREDATE":"12-MAR-81","SAL":950,"COMM":null,"DEPTNO":30},{"EMPNO":7902,"ENAME":"FORD","JOB":"ANALYST","MGR":7566,"HIREDATE":"12-MAR-81","SAL":3000,"COMM":null,"DEPTNO":20},{"EMPNO":7934,"ENAME":"MILLER","JOB":"CLERK","MGR":7782,"HIREDATE":"23-JAN-82","SAL":1300,"COMM":null,"DEPTNO":10}]}
Se även:
- Exportera data till Excel med PLSQL
- Exportera data till CSV med PL/SQL