Hello experts,
Oracle DB 12.2
I have the below scenario:
1. Input is an expression that needs to be validated for balanced parenthesis.
2. After step1, convert it into JSON like structure.
Eg: ((I=2 && J=10) || (K=1 && L=7))
Step1: The input string is balanced.
Step2: And it is converted like below:
{
"QUERY":{
"OR":{
"AND":{
"I":2,
"J":10
}
},
{
"AND":{
"K":1,
"L":7
}
}
}
}
I have attempted till here:
create or replace function f_is_balanced_str(i_str varchar2)
return pls_integer
deterministic
as
l_count pls_integer := 0;
l_ch char;
l_str varchar2(4000);
begin
l_str := translate(i_str, '()'||i_str, '()');
for i in 1 .. length(l_str)
loop
l_ch := substr(l_str, i, 1);
if l_ch = '(' then
l_count := l_count + 1;
elsif l_ch = ')' then
l_count := l_count - 1;
end if;
if l_count < 0 then
return 0;
end if;
end loop;
return case when l_count = 0 then 1 else 0 end;
end;
The above one validates the string. And I need to convert the string to JSON. Need guidance here.
Data can be present in a table.
with test_data(str) as
(
select '((I=2 && J=10) || (K=1 && L=7))' from dual union all
select '((I=2 || J=10) && (K=1 || L=7))' from dual union all
select '(I=2 || J=10)' from dual union all
select null from dual
)
select * from test_data;
Regards,
Ranagal