Skip to Main Content
  • Questions
  • Procedure to read the input array and exclude in select

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, manoj.

Asked: March 08, 2018 - 5:46 pm UTC

Last updated: March 10, 2018 - 5:02 am UTC

Version: 12c

Viewed 1000+ times

You Asked

Hi,
Have a question in accessing json array parameter in db proc. below is the example input json array.

'{"List":["name1","name2"]}'
or
["name1","name2"]

how to access this type of input data and need to apply filter with this data. Means have to exclude this name in the select query.

select * from emp where name not in (name1,name2);

Expectation is to read the names from the input parameter and apply that name to filter.

Please help to solve this problem. it would be great if you could give solution for this.
thanks,
Manoj

and Chris said...

You can use JSON_table to convert a JSON array to rows:

with rws as (
  select '{"List":["Steven","Nancy"]}' names from dual
) 
  select *
  from   rws, json_table (
    names, '$' columns (
      nested path '$.List[*]' columns (
        nm path '$'
      )
    )
  ) j;

NAMES                         NM       
{"List":["Steven","Nancy"]}   Steven   
{"List":["Steven","Nancy"]}   Nancy 


The nested path clause instructs it to convert each element of the array to a row.

Once you have this you can join it like a regular table:

with rws as (
  select '{"List":["Steven","Nancy"]}' names from dual
) 
  select j.nm, e.employee_id
  from   rws, json_table (
    names, '$' columns (
      nested path '$.List[*]' columns (
        nm path '$'
      )
    )
  ) j
  join   hr.employees e
  on     e.first_name = j.nm;

NM       EMPLOYEE_ID   
Steven             100 
Steven             128 
Nancy              108 

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Manojkumar, March 09, 2018 - 3:24 pm UTC

Thank you so much for the help. It will be very helpful for me in my project.

Thanks,
Manoj
Connor McDonald
March 10, 2018 - 5:02 am UTC

glad we could help

More to Explore

SQL

The Oracle documentation contains a complete SQL reference.