You Asked
Hi Team..Please help me on below scenario.
I have data in a file as below. It has two records Id and JsonString DELIMITED By "|
1001"|{"id":"1234","code":"test1","Version":v1,"time":"2017-08-31","rank":2,"device":"TEST1234TEST","response":"approve","notes":[{"user":"lory","time":"2017-09-07","note":"Test"}],"actions":[{"user":"dev","action":"investigate","time":"2017-09-07"}],"research":[{"code":"GJJJ","Score":100,"research_action":"SUSPECT"},{"code":"GJJJ","Score":100,"research_action":"SUSPECT"},{"code":"GJJJ","Score":100,"research_action":"SUSPECT"},{"code":"GJJJ","Score":100,"research_action":"SUSPECT"}],"level":"level2"},{"id":"5678","code":"test2","Version":v2,"time":"2017-07-31","rank":1,"device":"test234wtest56","response":"reject","notes":[{"user":"tom","time":"2017-08-07","note":"Test"}],"actions":[{"user":"pk","action":"approve","time":"2016-09-07"}],"research":[{"code":"yyy","Score":700,"research_action":"approve"},{"code":"ppp","Score":444,"research_action":"reject"},{"code":"zzz","Score":100,"research_action":"approve"},{"code":"zzz","Score":300,"research_action":"approve"}],"level":"level1"},{"id":"9876","code":"test3","Version":v5,"time":"2015-07-31","rank":6,"device":"junkdevice","response":"reject","notes":[{"user":"mary","time":"2017-09-07","note":"test23"}],"actions":[{"user":"romo","action":"suspet","time":"2016-09-07"}],"research":[{"code":"zzzz","Score":900,"research_action":"deney"},{"code":"xxx","Score":4545,"research_action":"approve"},{"code":"rrt","Score":300,"research_action":"approve"},{"code":"uuu","Score":340,"research_action":"approve"}],"level":"level4"}
I want to store the records in a table as below.
create table test0915(id number, response varchar2(4000));
Id response
===================================
100{"id":"1234","code":"test1","Version":v1,"time":"2017-08-31","rank":2,"device":"TEST1234TEST","response":"approve","notes":[{"user":"lory","time":"2017-09-07","note":"Test"}],"actions":[{"user":"dev","action":"investigate","time":"2017-09-07"}],"research":[{"code":"GJJJ","Score":100,"research_action":"SUSPECT"},{"code":"GJJJ","Score":100,"research_action":"SUSPECT"},{"code":"GJJJ","Score":100,"research_action":"SUSPECT"},{"code":"GJJJ","Score":100,"research_action":"SUSPECT"}],"level":"level2"}
1001{"id":"5678","code":"test2","Version":v2,"time":"2017-07-31","rank":1,"device":"test234wtest56","response":"reject","notes":[{"user":"tom","time":"2017-08-07","note":"Test"}],"actions":[{"user":"pk","action":"approve","time":"2016-09-07"}],"research":[{"code":"yyy","Score":700,"research_action":"approve"},{"code":"ppp","Score":444,"research_action":"reject"},{"code":"zzz","Score":100,"research_action":"approve"},{"code":"zzz","Score":300,"research_action":"approve"}],"level":"level1"}
1001{"id":"9876","code":"test3","Version":v5,"time":"2015-07-31","rank":6,"device":"junkdevice","response":"reject","notes":[{"user":"mary","time":"2017-09-07","note":"test23"}],"actions":[{"user":"romo","action":"suspet","time":"2016-09-07"}],"research":[{"code":"zzzz","Score":900,"research_action":"deney"},{"code":"xxx","Score":4545,"research_action":"approve"},{"code":"rrt","Score":300,"research_action":"approve"},{"code":"uuu","Score":340,"research_action":"approve"}],"level":"level4"}
and Connor said...
I'll assume you can load the data in via external table or SQL Loader (plenty of examples here on AskTOM of that), so here's the SQL that could follow that:
SQL> with t as
2 (
3 select '1001"|{"id":"1234","code":"test1","Version":v1,"time":"2017-08-31","rank":2,"device":"TEST1234TEST","response":"approve","n
user":"dev","action":"investigate","time":"2017-09-07"}],"research":[{"code":"GJJJ","Score":100,"research_action":"SUSPECT"},{"code":"GJ
,"research_action":"SUSPECT"},{"code":"GJJJ","Score":100,"research_action":"SUSPECT"}],"level":"level2"},{"id":"5678","code":"test2","Ve
nse":"reject","notes":[{"user":"tom","time":"2017-08-07","note":"Test"}],"actions":[{"user":"pk","action":"approve","time":"2016-09-07"}
code":"ppp","Score":444,"research_action":"reject"},{"code":"zzz","Score":100,"research_action":"approve"},{"code":"zzz","Score":300,"re
","Version":v5,"time":"2015-07-31","rank":6,"device":"junkdevice","response":"reject","notes":[{"user":"mary","time":"2017-09-07","note"
-07"}],"research":[{"code":"zzzz","Score":900,"research_action":"deney"},{"code":"xxx","Score":4545,"research_action":"approve"},{"code"
40,"research_action":"approve"}],"level":"level4"}' c from dual
4 )
5 select
6 substr(c,1,instr(c,'|')-2) id,
7 case
8 when instr(c,'{"id":',1,idx+1) > 0 then
9 substr(c,instr(c,'{"id":',1,idx),instr(c,'{"id":',1,idx+1)-instr(c,'{"id":',1,idx))
10 else
11 substr(c,instr(c,'{"id":',1,idx))
12 end str
13 from t,
14 lateral(select level idx from dual connect by level <= regexp_count(t.c,'{"id":'));
ID
----
STR
----------------------------------------------------------------------------------------------------------------------------------
1001
{"id":"1234","code":"test1","Version":v1,"time":"2017-08-31","rank":2,"device":"TEST1234TEST","response":"approve","notes":[{"user
":"lory","time":"2017-09-07","note":"Test"}],"actions":[{"user":"dev","action":"investigate","time":"2017-09-07"}],"research":[{"c
ode":"GJJJ","Score":100,"research_action":"SUSPECT"},{"code":"GJJJ","Score":100,"research_action":"SUSPECT"},{"code":"GJJJ","Score
":100,"research_action":"SUSPECT"},{"code":"GJJJ","Score":100,"research_action":"SUSPECT"}],"level":"level2"},
1001
{"id":"5678","code":"test2","Version":v2,"time":"2017-07-31","rank":1,"device":"test234wtest56","response":"reject","notes":[{"use
r":"tom","time":"2017-08-07","note":"Test"}],"actions":[{"user":"pk","action":"approve","time":"2016-09-07"}],"research":[{"code":
"yyy","Score":700,"research_action":"approve"},{"code":"ppp","Score":444,"research_action":"reject"},{"code":"zzz","Score":100,"re
search_action":"approve"},{"code":"zzz","Score":300,"research_action":"approve"}],"level":"level1"},
1001
{"id":"9876","code":"test3","Version":v5,"time":"2015-07-31","rank":6,"device":"junkdevice","response":"reject","notes":[{"user":"
mary","time":"2017-09-07","note":"test23"}],"actions":[{"user":"romo","action":"suspet","time":"2016-09-07"}],"research":[{"code":
"zzzz","Score":900,"research_action":"deney"},{"code":"xxx","Score":4545,"research_action":"approve"},{"code":"rrt","Score":300,"r
esearch_action":"approve"},{"code":"uuu","Score":340,"research_action":"approve"}],"level":"level4"}
3 rows selected.
Rating
(2 ratings)
Is this answer out of date? If it is, please let us know via a Comment