Skip to main content

JSON_TABLE

Unpacking JSON data in MySQL.

Introduction to JSON_TABLE

The JSON_TABLE function in MySQL is a powerful tool that allows you to transform JSON data into a tabular format. This is particularly useful when you need to query or manipulate JSON data as if it were part of a standard relational table. With JSON becoming a common data format for APIs and NoSQL-like storage, the ability to integrate it seamlessly into MySQL queries is essential for modern database management.

Key Benefits of JSON_TABLE:
  • It converts hierarchical JSON data into rows and columns, making it easier to handle within SQL queries.
  • Allows for complex JSON structures, including nested objects and arrays, to be flattened and queried using standard SQL techniques.
  • It works well in combination with other MySQL features such as joins, filters, and aggregations, enabling advanced data analysis.

In short, JSON_TABLE bridges the gap between structured relational data and flexible JSON formats, allowing developers to work with JSON data in a familiar SQL environment.

This section will explore how JSON_TABLE works, its syntax, and how to use it effectively in real-world scenarios.


Syntax of JSON_TABLE

The JSON_TABLE function in MySQL follows a structured syntax that defines how JSON data should be extracted and mapped to relational table columns. Understanding the syntax is crucial for effectively transforming and querying JSON data.

SELECT *
FROM JSON_TABLE(
  '[{"id": 1, "name": "John"}, {"id": 2, "name": "Doe"}]',
  '$[*]'
  COLUMNS (
    user_id INT PATH '$.id',
    user_name VARCHAR(50) PATH '$.name'
  )
) AS users;

In this example:

  • The JSON array contains two objects, each with an id and name.
  • JSON_TABLE transforms this into a relational table with two columns (user_id, user_name) by mapping the JSON keys id and name to the respective columns.

This section provides the foundation for understanding how JSON_TABLE operates by mapping JSON structures to relational table formats, preparing for more advanced use cases in the following sections.


Defining JSON Path Expressions

In the context of JSON_TABLE, JSON path expressions are used to navigate and extract specific parts of a JSON document. These path expressions follow a structured format that allows you to drill down into complex JSON objects and arrays, making it easier to map JSON data into relational columns.

Understanding JSON Path Expressions:
  • Root ($):

    • The JSON path starts with a $, representing the root of the JSON document. From here, you can navigate to specific keys or elements.

  • Dot Notation (.):

    • Use dot notation to access keys within the JSON object. For example, $.name extracts the value of the name key at the root level.

  • Array Indexing ([ ]):

    • Square brackets are used to access elements within JSON arrays. For example, $.items[0] accesses the first element in the items array.

  • Wildcard (*):

    • A wildcard * can be used to match all elements in an array or all keys within an object. For example, $[*] matches every element in an array, while $.data.* matches all keys within the data object.

Common Path Expressions:
  • Single Key Access:
$.key

Example: For JSON {"name": "John"}, the path $.name extracts the value "John".

  • Nested Key Access:
$.parent.child

Example: For JSON {"parent": {"child": "value"}}, the path $.parent.child extracts "value".

  • Array Element Access:
$.array[0]

Example: For JSON {"array": [10, 20, 30]}, the path $.array[0] extracts the first element, 10.

  • Accessing All Elements in an Array:
$[*]

Example: For a JSON array [{"id": 1}, {"id": 2}], the path $[*] will access all elements.

Using Path Expressions in JSON_TABLE:

You will define these JSON path expressions in the COLUMNS clause of the JSON_TABLE function to extract values into specific columns. Each column maps to a path expression, ensuring the correct data is extracted from the JSON.

SELECT *
FROM JSON_TABLE(
  '[{"id": 1, "name": {"first": "John", "last": "Doe"}}, {"id": 2, "name": {"first": "Jane", "last": "Smith"}}]',
  '$[*]'
  COLUMNS (
    user_id INT PATH '$.id',
    first_name VARCHAR(50) PATH '$.name.first',
    last_name VARCHAR(50) PATH '$.name.last'
  )
) AS users;

Explanation:

  • $[*]:
    • This matches all elements in the root JSON array.
  • $.id:
    • Extracts the id field from each object in the array.
  • $.name.first and $.name.last:
    • These paths navigate into the nested name object to extract first and last names.

By mastering JSON path expressions, you can effectively extract data from both simple and complex JSON structures in MySQL using JSON_TABLE. This enables you to manipulate JSON data just like traditional relational data.


Extracting Data with JSON_TABLE

Once you've defined the JSON path expressions, the next step is to use JSON_TABLE to extract data from your JSON document into a tabular format. This process involves mapping specific parts of the JSON data to corresponding columns in a result set. The extracted data can then be queried and manipulated just like any other relational data in MySQL.

Steps to Extract Data with JSON_TABLE:
  1. Specify the JSON Document:

    • The first parameter in JSON_TABLE is the JSON document or column from which data will be extracted. This can be:
      • A JSON string.
      • A JSON column from an existing table.
      • The result of a JSON-generating function (e.g., JSON_ARRAY, JSON_OBJECT).
  2. Define the Path Expression:

    • The second parameter is the JSON path expression, which specifies where in the JSON document the data is located.
    • Use $[*] if you want to extract data from all elements in a JSON array.
  3. Map Columns to JSON Data:

    • In the COLUMNS clause, define how each JSON field will map to a column in the result set.
    • For each column, provide:
      • A column name.
      • A data type (e.g., INT, VARCHAR, etc.).
      • A JSON path expression that tells MySQL where to extract the data from the JSON.
  4. Alias for the Result Table:

    • Give the resulting table an alias for easier reference in queries, just as you would with any subquery or derived table in SQL.
Example 1: Extracting Simple Data
SELECT *
FROM JSON_TABLE(
  '[{"id": 1, "name": "John"}, {"id": 2, "name": "Jane"}]', -- JSON data
  '$[*]'                                                    -- Path expression for array elements
  COLUMNS (
    user_id INT PATH '$.id',                                -- Extracting the "id" field
    user_name VARCHAR(50) PATH '$.name'                     -- Extracting the "name" field
  )
) AS users;

Result:

user_id user_name
1 John
2 Jane
  • Explanation:
    • The JSON document is an array with two objects, each containing an id and name.
    • JSON_TABLE flattens this data into a two-column table (user_id and user_name).
Example 2: Extracting Data from Nested JSON Objects

For more complex JSON structures, such as nested objects, you can define deeper path expressions to access the inner fields.

SELECT *
FROM JSON_TABLE(
  '[{"id": 1, "details": {"first_name": "John", "last_name": "Doe"}},
    {"id": 2, "details": {"first_name": "Jane", "last_name": "Smith"}}]', -- JSON with nested objects
  '$[*]'
  COLUMNS (
    user_id INT PATH '$.id',                                   -- Extracting the "id" field
    first_name VARCHAR(50) PATH '$.details.first_name',        -- Extracting the "first_name" from nested "details"
    last_name VARCHAR(50) PATH '$.details.last_name'           -- Extracting the "last_name" from nested "details"
  )
) AS users;

Result:

user_id first_name last_name
1 John Doe
2 Jane Smith
  • Explanation:
    • The details object contains first_name and last_name, so the path expressions $.details.first_name and $.details.last_name are used to access these values.
Example 3: Using FOR ORDINALITY:

When dealing with JSON arrays, FOR ORDINALITY can be added to generate an additional column that assigns a unique row number to each element of the array.

SELECT *
FROM JSON_TABLE(
  '[{"item": "Apple"}, {"item": "Banana"}, {"item": "Orange"}]',
  '$[*]'
  COLUMNS (
    row_number FOR ORDINALITY,                                 -- Adds row numbers
    item_name VARCHAR(50) PATH '$.item'                        -- Extracts item names
  )
) AS fruit_list;

Result:

row_number item_name
1 Apple
2 Banana
3 Orange
  • Explanation:
    • FOR ORDINALITY assigns a unique number to each array element, useful for indexing JSON array data.
Example 4: Joining JSON_TABLE Results with Other Tables

You can also join the results of JSON_TABLE with other relational tables.

SELECT u.user_id, u.user_name, o.order_id
FROM JSON_TABLE(
  '[{"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"}]',
  '$[*]'
  COLUMNS (
    user_id INT PATH '$.id',
    user_name VARCHAR(50) PATH '$.name'
  )
) AS u
JOIN orders o ON u.user_id = o.user_id;  -- Assuming there’s an 'orders' table

Result:

user_id user_name order_id
1 Alice 101
2 Bob 102
  • Explanation:
    • This example shows how to join the extracted JSON data with an existing orders table based on a common user ID.
Summary:

By extracting data with JSON_TABLE, you can flatten JSON structures, making it easier to work with JSON data in a relational format. This approach unlocks the ability to use standard SQL operations (e.g., joins, filters, and aggregates) on JSON data directly within MySQL.