MySQL

JSON_EXTRACT MySQL Function: Complete Guide

intro

Let’s explore everything you need to know about the JSON_EXTRACT MySQL function, a powerful tool for retrieving data from JSON documents.

Tools used in the tutorial
Tool Description Link
Dbvisualizer DBVISUALIZER
TOP RATED DATABASE MANAGEMENT TOOL AND SQL CLIENT

Modern databases like MySQL can not only store JSON data, but also let you query and programmatically access fields within it. In detail, MySQL provides several JSON search functions. Well, the JSON_EXTRACT MySQL function is one of the most powerful!

In this guide, you will learn what JSON_EXTRACT is, how it works, and what kind of data it allows you to retrieve.

Let’s dive in!

What Is the JSON_EXTRACT MySQL Function?

In MySQL, the JSON_EXTRACT function retrieves data from a JSON document by selecting the parts that match a given JSON path expression. (In case you are not familiar with that syntax, we will dig into the JSON path syntax later.)

Note that the definition mentions “parts” in the plural. That is because JSON_EXTRACT can return multiple fields from the JSON document, depending on the JSON path you provide.

MySQL JSON_EXTRACT: Syntax and Usage

This is the syntax of the MySQL JSON_EXTRACT function:

Copy
        
1 JSON_EXTRACT(json_doc, path[, path] ...)

Where:

  • json_doc is either a valid JSON document written as a string or the name of a column that stores data in the JSON data type.
  • path[, path] ...: is one or more JSON path expressions, separated by commas.

If the path list contains only a single JSON path expression, JSON_EXTRACT returns a single JSON value. For example:

Copy
        
1 SELECT JSON_EXTRACT('{"user": {"name": "Alice", "age": 30}}', '$.user.name');

In this case, the result will be:

Copy
        
1 "Alice"

Note: Those double quotes are there for a reason. JSON_EXTRACT returns a value of type JSON. So even if it looks like a regular string (and your client may display it as one), the returned data is always in valid JSON format.

If there are multiple path expressions, the MySQL JSON_EXTRACT function returns a JSON array of values, like in this example:

Copy
        
1 SELECT JSON_EXTRACT('{"user": {"name": "Alice", "age": 30}}', '$.user.name', '$.user.age');

This time, the result will be:

Copy
        
1 ["Alice", 30]

Notes:

  • JSON_EXTRACT returns NULL if any argument is NULL or if no path matches a value in the document.
  • The Invalid JSON text in argument 1 to function json_extract: "Missing a name for object member." error occurs if the json_doc argument is not valid JSON.
  • The Invalid JSON path expression error occurs if any of the path arguments are not valid path expressions.

MySQL JSON Path Syntax

If you already know what MySQL JSON Path is and how it works, feel free to skip this section.

Many MySQL JSON functions—including JSON_EXTRACT—require a JSON path expression to locate specific elements within a JSON document. That expression must be written following the MySQL JSON path specification.

A JSON path consists of a scope, followed by one or more path legs. Here are the key rules for writing JSON path expressions in MySQL:

  • The scope is always the root of the document and is represented by the dollar sign ($).
  • Path legs are separated by periods (.).
  • Array elements are accessed using square brackets with a zero-based index: [N], where N is a non-negative integer.
  • Object keys must be valid ECMAScript identifiers.

MySQL also supports the following wildcards:

  • .* matches all members of an object.
  • [*] matches all elements of an array.
  • [prefix]**suffix matches all paths that start with the optional prefix and end with the required suffix. Keep in mind that a path cannot end with *.

For example, consider this JSON document:

Copy
        
1 { 2 "user": { 3 "name": "Alice", 4 "age": 30, 5 "emails": ["alice@example.com", "a.smith@example.com"], 6 "address": { 7 "city": "New York", 8 "zip": "10001" 9 } 10 } 11 }

This is what different path expressions would produce:

  • $.user.name"Alice"
  • $.user.emails[0]"alice@example.com"
  • $.user.address.city"New York"
  • $.user.emails[*]["alice@example.com", "a.smith@example.com"]
  • $.user.address.*{"city": "New York", "zip": "10001"}
  • $**.zip"10001"

How to Extract a Field from a JSON Object in MySQL

Now that you understand how the JSON_EXTRACT MySQL function works and how to use MySQL's JSON path syntax, you are ready to see a real-world example.

Assume you have a table called config that stores each user's settings in a JSON column. Using a visual database MySQL client like DbVisualizer, you can easily explore the contents of this table:

Exploring the data in the config table in DbVisualizer
Exploring the data in the config table in DbVisualizer

As you can see, this sample table contains user settings stored in a MySQL JSON column named settings.

Now, let’s extract specific settings with JSON_EXTRACT in MySQL!

To get the theme setting of the user with id = 2, you can use the following query:

Copy
        
1 SELECT JSON_EXTRACT(settings, '$.theme') AS theme 2 FROM config 3 WHERE user_id = 2;

As you can see, the result will be "light":

Executing the JSON_EXTRACT query in DbVisualizer
Executing the JSON_EXTRACT query in DbVisualizer

Now, if you run the same query on a user whose JSON object doesn't contain a theme key, you will get NULL. For example:

Copy
        
1 SELECT JSON_EXTRACT(settings, '$.theme') AS theme 2 FROM config 3 WHERE user_id = 1;

See the NULL result:

Note the NULL result
Note the NULL result

Note that you can achieve the same result using the MySQL -> operator:

Copy
        
1 SELECT settings->'$.theme' AS theme 2 FROM config;

This time, you will get:

Note that you get the same result as before
Note that you get the same result as before

In particular, the -> operator is a shorthand for the MySQL JSON_EXTRACT function when used with two arguments.

Conclusion

In this guide, you learned what the JSON_EXTRACT MySQL function is, how it works, how to use it with advanced JSON path queries, and what kind of data it returns.

As demonstrated in a real-world example, DbVisualizer allows you to manage JSON data visually and intuitively. This is just one of its many powerful features—others include SQL formatting, ERD-style schema diagrams, query optimization tools, and more. Download DbVisualizer for free today!

FAQ

When was JSON support added to MySQL?

MySQL introduced basic support for the JSON data type in version 5.7.8, released in mid-2015. While this version allowed storing and retrieving JSON data, the functionality was limited. Significant improvements came with MySQL 8.0, released in April 2018. This version made JSON support far more practical and usable. So, while JSON was technically supported in MySQL 5.7, most of the features developers rely on today were added in version 8.0.

When was JSON_EXTRACT added to MySQL?

The JSON_EXTRACT MySQL function was introduced during the MySQL 5.7 development cycle. Although it existed in 5.7, it became much more useful and popular with the enhancements brought by MySQL 8.0. If you are still using MySQL 5.7 (which has now reached end-of-life and is no longer maintained), you can use JSON_EXTRACT, but you will be missing out on newer JSON-related features and performance improvements.

Do other databases support JSON?

Yes, several major relational databases support JSON, including:

  • PostgreSQL: Offers robust JSON support, including two data types: JSON and JSONB. Learn more in our guide on JSON vs JSONB in PostgreSQL.
  • SQL Server: Provides JSON functions for parsing and querying JSON strings, though it does not have a dedicated JSON column type as of the time of writing because there is a JSONdata type preview.
  • Oracle: Supports JSON natively and includes extensive querying capabilities.

What is the difference between the JSON_EXTRACT MySQL function and the ->/->> operators?

JSON_EXTRACT and the -> operator both return JSON values from a JSON column. The ->> operator, however, returns the unquoted scalar value (as plain text). In short:

  • -> = JSON_EXTRACT with two arguments
  • ->> returns unquoted scalar value (e.g., a string or number)

Why use a visual database client to deal with JSON data in MySQL?

A visual database client like DbVisualizer makes it easier to view, query, and explore nested JSON structures, which can be difficult to interpret in raw SQL output. It improves readability, debugging, and understanding of complex data without writing extra formatting queries. These are only some of the advanced features that a powerful database client can offer. Grab a 21-day free trial for DbVisualizer Pro to discover them all!

Dbvis download link img
About the author
Antonello Zanini

Antonello is a software engineer, and often refers to himself as a technology bishop. His mission is to spread knowledge through writing.

The Table Icon
Sign up to receive The Table's roundup
More from the table
Title Author Tags Length Published
title

Understanding MySQL Data Types: A Complete Guide for Beginners

author Lukas Vileikis tags DATA TYPES MARIADB MySQL 6 min 2025-08-20
title

SQL IS NOT NULL Condition: Definitive Guide

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 6 min 2025-08-13
title

Can a Primary Key Be NULL in an SQL Table?

author TheTable tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 5 min 2025-08-12
title

How Does LOAD DATA Work for MySQL?

author Antonello Zanini tags MySQL 10 min 2025-08-11
title

ISNULL vs COALESCE: Comparing NULL Handling Functions

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 6 min 2025-08-06
title

The SELECT INTO TEMP TABLE Mechanism in SQL

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 6 min 2025-08-05
title

Beyond COALESCE: SQL Clauses That Can Help You

author Lukas Vileikis tags DbVisualizer MySQL SQL 7 min 2025-07-29
title

A Guide to the CREATE TEMPORARY TABLE SQL Statement

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 7 min 2025-07-28
title

The Process of Compiling MySQL from Source

author Lukas Vileikis tags MARIADB MySQL SQL 6 min 2025-07-23
title

The RANK Window Function in SQL: A Complete Guide

author Leslie S. Gyamfi tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 6 min 2025-07-21

The content provided on dbvis.com/thetable, including but not limited to code and examples, is intended for educational and informational purposes only. We do not make any warranties or representations of any kind. Read more here.