Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Databases
  • SQL
  • MySQL
  • PostgreSQL
  • PL/SQL
  • MongoDB
  • SQL Cheat Sheet
  • SQL Interview Questions
  • MySQL Interview Questions
  • PL/SQL Interview Questions
  • Learn SQL and Database
Open In App
Next Article:
Math Functions in PL/SQL
Next article icon

JSON Functions in PL/SQL

Last Updated : 11 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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_objjson_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:

emp_name
Alice

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_nameemp_age
John35
Jane29

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.


Next Article
Math Functions in PL/SQL

A

abhay94517
Improve
Article Tags :
  • Databases
  • PL/SQL

Similar Reads

  • 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
  • MySQL JSON Functions
    JSON functions in MySQL allow working with JSON data in the database and retrieve JSON data from it. These functions help in storing and accessing JSON data efficiently and such data is widely used in web applications as it is flexible and easy to use. In this article, We will learn about the MySQL
    5 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
  • 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
  • LEAST() Function in MySQL
    The LEAST() function in MySQL is a versatile tool that returns the smallest (minimum) value from a list of expressions. It can be used with numbers, strings, or even columns of data to find the minimum value according to their data type. In this article, We will learn about LEAST() Function in MySQL
    4 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
  • SIGN() Function in SQL Server
    In SQL Server, the SIGN() function is a mathematical function used to determine the sign of a given numeric expression. This function is particularly useful when we want to evaluate whether a number is positive, negative or zero. In this article, We will learn about SIGN() Function in SQL Server in
    3 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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences