DEV Community

Cover image for Oracle JSON_TABLE: Transforming JSON into Relational Format
Ricardo
Ricardo

Posted on • Originally published at rmauro.dev on

Oracle JSON_TABLE: Transforming JSON into Relational Format

The function JSON_TABLE allows us to transform JSON data into a relational format.

This article will explore the Oracle JSON_TABLE function and its syntax and provide examples to demonstrate its usage and capabilities.

Table of Contents

Understanding Oracle JSON_TABLE

The JSON_TABLE function in Oracle converts JSON data into a relational format by extracting specified JSON data elements and returning them as rows and columns.

It enables you to query and process JSON data using SQL techniques, making integrating JSON and relational data easier.

The Syntax of Oracle JSON_TABLE:

JSON_TABLE(
    json_data
    , path_expression COLUMNS (column_name data_type PATH json_path_exp) [, ...])

Enter fullscreen mode Exit fullscreen mode
  • json_data: The JSON document or column from which to extract data.
  • path_expression: The JSON path expression specifies the elements to extract.
  • column_name: The name to assign to the extracted JSON element.
  • data_type: The data type of the extracted JSON element.
  • json_path_exp: The JSON path expression to access the specific element within the JSON structure.

Example Using Oracle JSON_TABLE

Let's consider a practical example to illustrate the usage of Oracle JSON_TABLE. Suppose we have a JSON document representing a list of employees and their details:

{
  "employees": [
    { "name": "John Doe", "age": 30, "position": "Software Engineer" },
    { "name": "Jane Smith", "age": 35, "position": "Project Manager" } 
  ]
}
Enter fullscreen mode Exit fullscreen mode

We can use JSON_TABLE to extract the employee details and transform them into a relational format:

SELECT A.NAME, A.AGE, A.POSITION
FROM JSON_TABLE(
    JSON_VALUE
    , '$.employees[*]' COLUMNS (
        NAME VARCHAR PATH '$.name',
        AGE NUMBER PATH '$.age',
        POSITION VARCHAR PATH '$.position'
    )
) A
Enter fullscreen mode Exit fullscreen mode

Usage of JSON_TABLE

In the above example, we use the JSON_TABLE function to extract the "name," "age," and "position" elements from the "employees" array.

Using the COLUMNS clause, we assign them to corresponding columns with appropriate data types.

The resulting query retrieves the employee details in a relational format.

Additional Considerations

  • The JSON_TABLE function supports advanced features such as nested JSON structures, array handling, and object flattening.
  • You can use SQL functions and expressions within the JSON_TABLE statement to perform calculations or transformations on extracted data.
  • JSON_TABLE also supports error handling and handling missing or optional JSON elements.

Conclusion

Oracle JSON_TABLE is a powerful function that enables the transformation of JSON data into a relational format.

By extracting specified JSON elements and returning them as rows and columns, JSON_TABLE allows you to integrate JSON and relational data seamlessly.

Understanding the syntax and usage of Oracle JSON_TABLE empowers developers to efficiently query and process JSON data within an Oracle database environment.

Top comments (0)