Oracle

Chapter 7 - DQL (Data Query Language)

JSON SELECT

Oracle Database provides robust support for JSON, allowing you to query and manipulate JSON data directly within SQL. The JSON_SELECT function helps retrieve data stored in JSON format. This capability is especially useful when working with applications that generate JSON data, making it easier to integrate with modern web services and APIs. Below are key points to understand how to use JSON in your SQL queries.

  1. Understanding JSON Data Types

    • Oracle supports JSON data types natively.
    • JSON can be stored in columns with the VARCHAR2, CLOB, or BLOB data types.
  2. Basic JSON Query Syntax

    • Use JSON_VALUE to extract a scalar value.
    • Use JSON_QUERY to retrieve an object or array.
  3. Example of a JSON SELECT Query

    • Assume you have a table books that contains a column book_data storing JSON information about books.
    SELECT
        JSON_VALUE(book_data, '$.title') AS title,
        JSON_VALUE(book_data, '$.author') AS author
    FROM
        books
    WHERE
        JSON_EXISTS(book_data, '$.published_date');
    
  4. Querying Nested JSON Objects

    • You can query nested JSON structures using dot notation.
    SELECT
        JSON_VALUE(book_data, '$.publisher.name') AS publisher_name
    FROM
        books
    WHERE
        JSON_VALUE(book_data, '$.publisher.location') = 'New York';
    
  5. Aggregating JSON Data

    • Use JSON_ARRAYAGG to create a JSON array from selected rows.
    SELECT
        JSON_ARRAYAGG(book_data) AS all_books
    FROM
        books
    WHERE
        JSON_EXISTS(book_data, '$.genre') AND JSON_VALUE(book_data, '$.genre') = 'Fiction';
    
  6. Best Practices

    • Indexing: Consider using functional indexes on JSON attributes to improve query performance.
    • Validation: Ensure JSON data integrity by validating its structure before inserting it into the database.
    • Storage: Use appropriate data types based on the expected size and structure of JSON data.

By understanding these concepts and using the provided examples, you can effectively query JSON data in Oracle, enhancing your application's capabilities in handling modern data formats.

Tansy SQL Course | JSON SELECT | Chapter 7 | Lesson 31 - Video Thumbnail

TEST CODE

To select JSON data from an Oracle table and extract specific fields, use the following query:

SELECT client_id, first_name, last_name,
       complete_address,
       JSON_VALUE(complete_address, '$.address1') AS address1,
       JSON_VALUE(complete_address, '$.County') AS county,
       JSON_UNQUOTE(JSON_VALUE(complete_address, '$."PO BOX"')) AS po_box
FROM org_client
WHERE complete_address IS NOT NULL;

To select JSON data from an Oracle table and extract specific fields, use the following query:

SELECT client_id, first_name, last_name,
       JSON_VALUE(complete_address, '$.address1') AS address1,
       JSON_VALUE(complete_address, '$.County') AS county
FROM org_client
WHERE JSON_VALUE(complete_address, '$."PO BOX"') = '432';

To select JSON data from an Oracle table and extract specific fields, use the following query:

SELECT client_id, first_name, last_name,
       complete_address,
       JSON_VALUE(complete_address, '$.address1') AS address1,
       JSON_VALUE(complete_address, '$.County') AS county
FROM org_client
WHERE JSON_VALUE(complete_address, '$."PO BOX"') = '432';

In Oracle, you can use the `JSON_OBJECT` function to convert tabular data into JSON format. Here's an example query:

SELECT JSON_OBJECT(
           'client_id' VALUE client_id,
           'info' VALUE JSON_OBJECT(
               'name' VALUE first_name,
               'surname' VALUE last_name
           ),
           'birth_year' VALUE birth_year
       ) AS json_result
FROM org_client
WHERE client_id = 1;

SELECT JSON DATA

The following SQL queries demonstrate how to extract key-value pairs from a specified JSON column. Additionally, these queries illustrate the utilization of JSON key-value pairs within the WHERE clause.

RAW DATA WITH JSON DATA TYPE

RAW DATA WITH JSON DATA TYPE

MySQL QUERY

SELECT client_id, first_name, last_name,complete_address
    complete_address -> '$.address1',
    complete_address ->> '$.County',
    JSON_UNQUOTE(complete_address -> '$."PO BOX"')
FROM org_client
where complete_address IS NOT NULL;

POSTGRESQL QUERY

SELECT client_id, first_name, last_name,complete_address -> 'address1' AS address1
    complete_address ->> 'County' AS county
FROM org_client
where complete_address ->> 'PO BOX' = '432';

MS SQL SERVER AND ORACLE QUERY

SELECT client_id, first_name, last_name, complete_address
    JSON_VALUE(complete_address, '$.address1') AS address1,
    JSON_VALUE(complete_address, '$.County') AS county
FROM org_client
where JSON_VALUE(complete_address, '$."PO BOX"') = '432';

QUERY DATA MAPPING

QUERY DATA MAPPING

QUERY OUT PUT

QUERY OUT PUT
Comments(0 comments)

Comments Not Found