Friday, April 17, 2015

JSON for APEX Developers (part 3): Querying JSON in Oracle DB 12c

In previous blog posts I talked about JSON for APEX Developers:

In this post I want to show how you store and query JSON data straight in the database.

To start, create a table to store the JSON object:

    date_loaded DATE,
    json_document CLOB
    CONSTRAINT ensure_json CHECK (json_document IS JSON));

I inserted a couple of records in the table:

Here's in more detail the JSON in the json_document column:

Now to query the JSON data I can do :

You basically say j (table) . json_document (column) . analyses (first entry in JSON) . ... (other fields of the hierarchy you want to navigate to)
You see the data I get back is actually two records as I actually get back the JSON array.

To go into the array and see for example the first record, I can use json_value:

Note that the array start with 0.

And finally to see both records that are in the JSON array I can make use of json_table like this:

The above is just a grasp of what you can do with JSON directly in the database (12c) by using SQL.
If you're interested to read more about how to manipulate JSON in the database, have a look at the documentation, it contains a wealth on information with great examples.

As you can basically query the JSON with SQL you can use this SQL in your APEX reports... in the coming days I'll show you a real case where I used the above techniques to do some interesting searching in data.


Anonymous said...

Just executed in APEX SQL Workshop a query:

select JSON('{"foo":3,"bar":[1,2,3,4]}') from dual

Result is :


1 rows returned in 0.00 seconds

But there is no JSON function in Oracle 12c function list!
Also there is no such a name in all_objects and all_synonyms!

How can you explain this?

Thanks in advance,

Dimitri Gielis said...

Hi Andrew,

Not sure where you saw I did JSON() in my example?

In your table you define a CHECK CONSTRAINT CHECK IS JSON.

If you want to parse JSON, you can do:
apex_json.parse('{ "type": "circle", "coord": [10, 20] }');
sys.htp.p('Point at '||

Hope that helps,

Anonymous said...

I mean not apex_json packcage, I mean Oracle 12c itself.It's wonderful to get result
from undocummented function. I want to hear your opinion about this "phantom" function)

Thanks in advance,

Dan McGhan said...

Hi Andrew,

I'm guessing you've installed PL/JSON, which adds JSON, JSON_ARRAY, and a couple other object types:

Check your object types to verify.

I wrote about using PL/JSON for creating JSON here:


nitindeokate said...

I get this error
ORA-00904: "MSG"."MESSAGES": invalid identifier
00904. 00000 - "%s: invalid identifier"
Error at Line: 1 Column: 7

nitindeokate said...

fyi i am using 11g. If it is not supported, is there a way? i need this badly


pranav shah said...

Hello Dimitri Gielis ,

Following your blog since a long time but this my first ever comment :)
i am trying to integrate RESTfull services with HTML 5 or MAF (Mobile Application Framework) is it possible to do in such a manner that i can access all my data of oracle Apex 5.0 in MAF or HTML5.

Dimitri Gielis said...

sure you can do that, that is why REST webservice are there :)

iris said...

I would recommend to use JSON tool for debugging JSON data and

Matthew Cardozo said...


Thanks for your many helpful posts related to APEX.

I've been reading through your series of posts on using APEX to create and consume JSON and have a couple of questions...

I have an apex application where currently I'm getting a csv file from another group and uploading it in to a table on my schema daily. I'd like to automate this using REST services.

1. which method would be recommended from the APEX side: a GET to pull the JSON or POST to receive the JSON?
2. I see in this post that you have a table of json files in clob this a preferred method for dealing with the JSON if I'm planning to put the contained data into another table anyway? What would I need to do to store the JSON to this type of store table (requires a conversion etc?)

If you've already posted about this in another blog I apologize and would appreciate a link to said post.