You Asked
HI,
i have a json file, i have to insert into one table it's have multiple columns, How will convert into as record?
Please see the below example
Example:
{"EMPNO":123,"ENAME":"TEST","JOB":"ENGINEER","MGR":null,"HIREDATE":null,"SAL":null,"COMM":null,"DEPTNO":null}
{"EMPNO":3,"ENAME":"harish","JOB":"developer","MGR":null,"HIREDATE":null,"SAL":null,"COMM":null,"DEPTNO":null}
{"EMPNO":555,"ENAME":"TESTTTT","JOB":"SOFTWARE","MGR":null,"HIREDATE":null,"SAL":null,"COMM":null,"DEPTNO":null}
{"EMPNO":102,"ENAME":"harish","JOB":"developer","MGR":null,"HIREDATE":null,"SAL":null,"COMM":null,"DEPTNO":null}
{"EMPNO":444,"ENAME":"TESTTTT","JOB":"ENR2222","MGR":null,"HIREDATE":null,"SAL":null,"COMM":null,"DEPTNO":null}
{"EMPNO":700,"ENAME":"PANIPOORI","JOB":"CRICKET","MGR":null,"HIREDATE":null,"SAL":null,"COMM":null,"DEPTNO":null}
{"EMPNO":101,"ENAME":"harish","JOB":"developer","MGR":null,"HIREDATE":null,"SAL":null,"COMM":null,"DEPTNO":null}
i want to insert into emp table or any other ways to create insert scripts based on the Json file?
Thanks
Praveen
with LiveSQL Test Case:
and Chris said...
You can use JSON_table to convert JSON -> rows and columns, for example:
create table t (
c1 varchar2(1000)
check ( c1 is json )
);
insert into t
with rws as (
select '{"EMPNO":123,"ENAME":"TEST","JOB":"ENGINEER","MGR":null,"HIREDATE":null,"SAL":null,"COMM":null,"DEPTNO":null}' j from dual union all
select '{"EMPNO":3,"ENAME":"harish","JOB":"developer","MGR":null,"HIREDATE":null,"SAL":null,"COMM":null,"DEPTNO":null}' j from dual union all
select '{"EMPNO":555,"ENAME":"TESTTTT","JOB":"SOFTWARE","MGR":null,"HIREDATE":null,"SAL":null,"COMM":null,"DEPTNO":null}' j from dual union all
select '{"EMPNO":102,"ENAME":"harish","JOB":"developer","MGR":null,"HIREDATE":null,"SAL":null,"COMM":null,"DEPTNO":null}' j from dual union all
select '{"EMPNO":444,"ENAME":"TESTTTT","JOB":"ENR2222","MGR":null,"HIREDATE":null,"SAL":null,"COMM":null,"DEPTNO":null}' j from dual union all
select '{"EMPNO":700,"ENAME":"PANIPOORI","JOB":"CRICKET","MGR":null,"HIREDATE":null,"SAL":null,"COMM":null,"DEPTNO":null}' j from dual union all
select '{"EMPNO":101,"ENAME":"harish","JOB":"developer","MGR":null,"HIREDATE":null,"SAL":null,"COMM":null,"DEPTNO":null}' j from dual
)
select *
from rws;
commit;
select j.* from t, json_table (
c1, '$'
columns (
empno int path '$.EMPNO',
ename varchar2(20) path '$.ENAME'
)
) j;
EMPNO ENAME
123 TEST
3 harish
555 TESTTTT
102 harish
444 TESTTTT
700 PANIPOORI
101 harish
From there just insert the results of the query to the target table.
You need to know the attribute names to do this though. If you want something that pulls out the attributes for you, from 12.2 you can use the JSON Data Guide.
To use this, add a JSON search index, use DBMS_JSON to create the view, and viola:
create search index ji
on t ( c1 )
for json
parameters ( 'dataguide on' );
exec dbms_json.create_view_on_path ( 'emp_v', 't', 'c1', '$' );
select * from emp_v;
C1$JOB C1$MGR C1$SAL C1$COMM C1$EMPNO C1$ENAME C1$DEPTNO C1$HIREDATE
ENGINEER <null> <null> <null> 123 TEST <null> <null>
developer <null> <null> <null> 3 harish <null> <null>
SOFTWARE <null> <null> <null> 555 TESTTTT <null> <null>
developer <null> <null> <null> 102 harish <null> <null>
ENR2222 <null> <null> <null> 444 TESTTTT <null> <null>
CRICKET <null> <null> <null> 700 PANIPOORI <null> <null>
developer <null> <null> <null> 101 harish <null> <null>
Rating
(3 ratings)
Is this answer out of date? If it is, please let us know via a Comment