JSON (JavaScript Object Notation) has become more popular in modern applications, and databases like Oracle support JSON data. JSON is widely used in modern applications for its simplicity and flexibility, making it a popular format for exchanging data between systems.
In this article, we will explain the JSON functions in PL/SQL, their basic structure, and examples. Also show how to work easily and effectively with JSON data in Oracle databases and conversion of JSON data to relational data.
JSON Functions in PL/SQL
PL/SQL offers built-in JSON functions to work with JSON data. These functions help store, query, and modify JSON easily. These functions help create, extract, update, and validate JSON data with ease. which allows developers to perform complex operations on JSON fields important for developers handling dynamic or semi-structured data.
Key Features of JSON Functions in Oracle
- JSON Data Handling: Simplifies the storage, querying, and validation of JSON data within Oracle databases.
- Seamless Integration: Allows developers to handle semi-structured JSON data alongside traditional relational data, making it easy to work with modern web applications.
- Performance: Oracle optimizes queries involving JSON data for performance, especially with indexing.
Commonly Used JSON Functions in PL/SQL
Here are the most commonly used JSON functions in Oracle PL/SQL, which enable us to create, extract, and manipulate JSON data:
JSON_OBJECT
It Creates a JSON object from the specified key-value pairs.
SELECT JSON_OBJECT('key' VALUE expression) AS json_output FROM dual;
JSON_ARRAY
Creates a JSON array from the given set of values.
SELECT JSON_ARRAY(value1, value2, ...) AS json_array FROM dual;
JSON_VALUE
Extracts a scalar value from a JSON document.
SELECT JSON_VALUE(json_column, 'json_path') AS value FROM table_name;
JSON_QUERY
Extracts an object or an array from a JSON document.
SELECT JSON_QUERY(json_column, 'json_path') AS json_output FROM table_name;
JSON_TABLE
Converts JSON data to relational data (rows and columns).
SELECT * FROM JSON_TABLE( json_column, '$.json_path' COLUMNS ( column_name1 PATH 'path1', column_name2 PATH 'path2' )) jt;
Example 1: Using JSON_OBJECT
and JSON_ARRAY
In this example, we will demonstrate how to create a JSON object and a JSON array in PL/SQL using the JSON_OBJECT
and JSON_ARRAY
functions.
Query:
SELECT
JSON_OBJECT('id' VALUE 101, 'name' VALUE 'John Doe') AS json_obj,
JSON_ARRAY(10, 20, 30) AS json_array
FROM dual;
Output:
json_obj | json_array |
---|
{"id": 101, "name": "John Doe"} | [10, 20, 30] |
Explanation:
- The
JSON_OBJECT
function creates a JSON object with two key-value pairs: "
id
":
101
and "
name
": "
John Doe
"
.
- The
JSON_ARRAY
function creates an array with three values: 10, 20, 30
.
Example 2: Extracting Values with JSON_VALUE
Let’s say we have a table called Employees
that stores employee information in JSON format. Suppose We want to extract a specific employee’s name from the JSON data using the JSON_VALUE
function.
Query:
CREATE TABLE Employees (
emp_id INT PRIMARY KEY,
emp_data CLOB
);
INSERT INTO Employees (emp_id, emp_data)
VALUES (1, '{"name": "Alice", "age": 30, "department": "HR"}');
SELECT JSON_VALUE(emp_data, '$.name') AS emp_name
FROM Employees
WHERE emp_id = 1;
Output:
Explanation:
- The JSON_VALUE function is used to get the value of the name key from the emp_data JSON column for the employee whose emp_id is 1.
Example 3: Using JSON_TABLE
to Convert JSON to Relational Data
The JSON_TABLE function allows you to convert JSON data into relational data. In this example, we will extract employee information from a JSON string and present it in a tabular format.
Query:
SELECT *
FROM JSON_TABLE(
'{"employees": [{"name": "John", "age": 35}, {"name": "Jane", "age": 29}]}',
'$.employees[*]'
COLUMNS (
emp_name VARCHAR2(20) PATH '$.name',
emp_age NUMBER PATH '$.age'
)
) jt;
Output:
emp_name | emp_age |
---|
John | 35 |
Jane | 29 |
Explanation:
- The
JSON_TABLE
function extracts each employee’s name and age from the JSON document and presents the data in a relational format (rows and columns).
Example 4: Validating JSON Data with IS JSON Function
We can use the IS JSON
function to check whether a field contains valid JSON data. This function is useful when we need to validate input data in a column.
Query:
CREATE TABLE Products (
product_id INT PRIMARY KEY,
product_info CLOB
);
INSERT INTO Products (product_id, product_info)
VALUES (1, '{"product_name": "Laptop", "price": 1200}');
SELECT product_info
FROM Products
WHERE product_info IS JSON;
Output:
product_info |
---|
{"product_name": "Laptop", "price": 1200} |
Explanation:
- The
IS JSON
function confirms whether the product_info
column contains a valid JSON object. In this case, the query returns the product information for rows where the JSON structure is correct.
Conclusion
In this article, we explained the various JSON functions in PL/SQL that help in working with JSON data stored in Oracle databases. These functions simplify the process of creating, extracting, transforming, and validating JSON data within our PL/SQL programs.
With JSON becoming increasingly popular, understanding these functions is crucial for any developer working with modern applications and semi-structured data. The IS JSON function is useful for validating JSON fields, ensuring the data integrity.
Similar Reads
PL/SQL MIN() Function
PL/SQL means Procedural Language / relational Structured Query Language, the extended language of Oracle. It is primarily used to manage and manipulate databases. One of the most frequently utilized SQL functions is the MIN() function. This powerful aggregate function is essential for finding the sm
6 min read
Math Functions in PL/SQL
In PL/SQL, mathematical functions play a important role in performing calculations and manipulating numeric data. These functions allow us to execute a wide range of mathematical operations from basic arithmetic to complex computations within our PL/SQL code. In this article, we will learn about Mat
4 min read
PL/SQL Functions
PL/SQL functions are reusable blocks of code that can be used to perform specific tasks. They are similar to procedures but must always return a value. A function in PL/SQL contains:Function Header: The function header includes the function name and an optional parameter list. It is the first part o
4 min read
Window Functions in PL/SQL
In Oracle PL/SQL, analyzing and managing complex data relationships often involves performing calculations across sets of rows. This is where window functions, sometimes referred to as "Analytic functions," come into play. They enable powerful data analysis, such as sales forecasting, time-series an
7 min read
SQL MIN() Function
The MIN() function in SQL is a powerful tool that allows us to determine the smallest or lowest value from a specified column or expression. It is widely used in data analysis to extract minimum values for decision-making, reporting, and business insights. This function automatically excludes NULL v
8 min read
PL/SQL MAX() Function
The PL/SQL MAX() function is an essential aggregate function in Oracle databases, enabling users to efficiently determine the largest value in a dataset. Whether working with numerical data, dates, or strings, the MAX() function is flexible and widely applicable. In this article, we will provide a d
4 min read
Functions in LISP
A function is a set of statements that takes some input, performs some tasks, and produces the result. Through functions, we can split up a huge task into many smaller functions. They also help in avoiding the repetition of code as we can call the same function for different inputs. Defining Functio
3 min read
SQL General Functions
SQL general functions are built-in tools provided by SQL databases to perform a variety of operations on data. These functions are crucial for manipulating, analyzing, and transforming data efficiently. In this article, We will learn about the what are the SQL General Functions along with the exampl
5 min read
PL/SQL SUM() Function
The SUM() function in PL/SQL is used to calculate the sum of the numeric column. It is an aggregate function that performs calculations on a set of values and returns a single value. In this article, we will explore the syntax, usage, and examples of the PL/SQL SUM() function to help us understand i
5 min read
PL/SQL AVG() Function
The PL/SQL AVG() function serves as a powerful tool for performing aggregate calculations on numeric datasets within a database. By allowing developers to calculate average values while excluding NULL entries, it enhances data analysis capabilities. In this article, we will explore the AVG() functio
5 min read