JSON In Oracle Database

Swadhin Ray
CERTIFIED EXPERT
Published:
In this article, we'll take a closer look at JSON in Oracle SQL and how you can use it in your applications.

JSON (JavaScript Object Notation) is a popular data interchange format that has gained widespread adoption in recent years. It is a lightweight and flexible format that is easy to read and write, making it ideal for use in web applications, mobile apps, and other scenarios where data needs to be transmitted between different systems.

Oracle SQL has supported JSON data types since version 12.1, which means you can store JSON data in Oracle tables and query it using SQL. 


JSON Data Types in Oracle SQL

Oracle SQL supports two JSON data types: VARCHAR2 and CLOB. These data types can store JSON data as strings, with VARCHAR2 being used for shorter strings and CLOB for longer strings.

To create a table that stores JSON data, you can use the following syntax:

CREATE TABLE my_table (id NUMBER, json_data CLOB);

This creates a table called "my_table" with two columns: "id" and "json_data". The "json_data" column is defined as a CLOB data type, which means it can store large JSON strings.


Working with JSON Data in Oracle SQL

Once you have a table that stores JSON data, you can use SQL to query and manipulate the data. Oracle SQL provides a number of functions for working with JSON data, including:

  • JSON_VALUE: This function extracts a scalar value from a JSON string.
  • JSON_QUERY: This function extracts a JSON object or array from a JSON string.
  • JSON_TABLE: This function converts a JSON array into a table.

Here's an example of using the JSON_VALUE function to extract a value from a JSON string:


SELECT JSON_VALUE(json_data, '$.name') AS name FROM my_table;

This query extracts the value of the "name" field from the JSON data stored in the "json_data" column of the "my_table" table.


Working with Nested JSON Data

JSON data can be nested, with objects and arrays containing other objects and arrays. Oracle SQL provides functions for working with nested JSON data, including:

  • JSON_OBJECT: This function creates a JSON object from key-value pairs.
  • JSON_ARRAY: This function creates a JSON array from a list of values.
  • JSON_TABLE: This function can handle nested JSON data and convert it into a table.

Here's an example of using the JSON_TABLE function to query a nested JSON object:

SELECT j.*
FROM my_table t, JSON_TABLE(t.json_data, '$.items[*]' 
    COLUMNS (
        id NUMBER PATH '$.id',
        name VARCHAR2(50) PATH '$.name',
        price NUMBER PATH '$.price'
    )
) j
WHERE t.id = 1;

This query extracts data from a nested JSON object that contains an array of items. The JSON_TABLE function is used to convert the array into a table, which can then be queried using SQL.


Conclusion

JSON is a powerful and flexible data format that is widely used in modern web and mobile applications. With support for JSON data types and functions, Oracle SQL makes it easy to work with JSON data in your applications. Whether you're querying simple JSON objects or working with complex nested data structures, Oracle SQL has the tools you need to get the job done.


0
810 Views
Swadhin Ray
CERTIFIED EXPERT

Comments (0)

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.