Friday, August 12, 2016

Consuming a REST Web Service returning JSON in APEX

In APEX you can define a web service that returns XML as below - all declarative, just a few steps through a wizard.


Then generate a report on top of that web service - again just a few clicks through a wizard. The generated query looks like this:

select xtab."customerName"
     , xtab."customerId"
  from apex_collections c, 
          XMLTable('/Response/S_getCustomerListTableArray/S_getCustomerListArrayItem' passing xmltype001
            COLUMNS "customerName" PATH 'customerName'
                  , "customerId"   PATH 'customerId'
          ) xtab
 where c.collection_name = 'CUSTOMERLIST'

So the result of the web service is stored in an XMLTYPE column. And it's easy to spot where you're definitions for the Response XPath and Output Parameters are used.

But what if your web service returns JSON - as more and more web services will do so? If you switch the Output Format of the web service definition to JSON, the Response XPath property and the Output Parameters are not enterable. And if you generate a report on top of that web service, the resulting query is a - disappointing - 

select c.clob001
from apex_collections c
where c.collection_name = 'CUSTOMERLIST'

and your report will show a JSON dump. Not exactly what you're hoping for. And notice the result is stored in a CLOB.

But since 12c, the JSON support in the Oracle database has improved a lot and we can easily use that in our query. So if we rewrite our query to 

select cust.*
from   apex_collections
,      json_table(clob001, '$.Response.S_getCustomerListTableArray.S_getCustomerListArrayItem[*]'
       columns ( "customerName"   PATH '$.customerName'
               , "customerId"     PATH '$.customerId'
               )) cust
where collection_name = 'CUSTOMERLIST'

we will get the same result as the - old fashioned - XML one! And as you can see the code is very much alike and could be generated by APEX itself just like the XML version.

So I expect that in a future version this functionality will be added to the APEX builder - it shouldn't be that hard to implement! And maybe the apex_collections can have a real "JSON" column as well (in fact it would still be a CLOB with an "IS JSON" check).

The only prerequisite is, you have to run APEX on version 12c of the database. If you want to know more about JSON in the database, there is a serie of blog posts about this, starting here.
Post a Comment