Questions tagged [mysql-json]

DO NOT USE FOR BASIC JSON. Use for questions about the JSON data type in MySQL 5.7 or later. Use with the [mysql] tag.

Overview

MySQL 5.7.8 introduced a new native json data type.

This data type allows the developer to store and manipulate JSON-encoded data more efficiently, in comparison to JSON data stored as the more common string data type.

Insert

Data inserted must be valid JSON syntax

INSERT INTO t1 VALUES('{"key1": "value1", "key2": "value2"}');

Select

Retrieving data directly from a JSON column

mysql> SELECT JSON_EXTRACT('{"id": 14, "name": "Aztalan"}', '$.name');
+---------------------------------------------------------+
| JSON_EXTRACT('{"id": 14, "name": "Aztalan"}', '$.name') |
+---------------------------------------------------------+
| "Aztalan"                                               |
+---------------------------------------------------------+

Create table example

The following is a data-definition statement that defines an auto-generated JSON Object that reflects the corresponding values of the data row in the target table.

CREATE TABLE triangle (
  sidea     DOUBLE,
  sideb     DOUBLE,
  sidec     DOUBLE AS (SQRT(sidea * sidea + sideb * sideb)),
  rowdata   JSON AS (JSON_OBJECT("aa",sidea, "bb",sideb, "cc",sidec ))
);
INSERT INTO triangle (sidea, sideb) VALUES(1,1),(3,4),(6,8);    

## Table
1   1   1.4142135623730951  {"aa": 1, "bb": 1, "cc": 1.4142135623730951}
3   4   5   {"aa": 3, "bb": 4, "cc": 5}
6   8   10  {"aa": 6, "bb": 8, "cc": 10}

Basic functions

--- Query
SELECT JSON_Array(
  'alpha'      
  ,'bravo'    
  ,'charlie'
  ,'delta'
) AS Result;          

--- Result          
["alpha", "bravo", "charlie", "delta"]

--- Query
SELECT JSON_OBJECT(
  'alpha',      'one'
  ,'bravo',     'two'
  ,'charlie',   '3'
  ,'delta',     004
) AS Result;

--- Result          
{"alpha": "one", "bravo": "two", "delta": 4, "charlie": "3"}

See also

386 questions
9
votes
5 answers

How to convert MySQL JSON array to comma separated string

I have the following phone numbers in a column: ["+63(02)3647766", "+63(02)5467329", "+63(02)8555522", "+63(02)3642403"] How can I get that info like this: +63(02)3647766,+63(02)5467329,+63(02)8555522,+63(02)3642403
Mihai Vinaga
  • 1,059
  • 2
  • 10
  • 27
8
votes
3 answers

MySQL search json value by key in array

I have an array of JSON objects and want to have a specific node returned. To simplify my problem, lets say the array could look like this: [ {"Race": "Orc", "strength": 14}, {"Race": "Knight", "strength": 7}, ... ] And I want to know…
DataVader
  • 740
  • 1
  • 5
  • 19
8
votes
4 answers

PHP PDO query error on table has json data type (MySQL 5.7.8-rc)

I'm trying new json datatype on mysql 5.7. When I use native php mysql query, it works fine, but when I use PDO to query data, it shows this error: Error: exception 'PDOException' with message 'SQLSTATE[HY000]: General error: 2036 ' in…
Quang Ng.
  • 81
  • 1
  • 4
7
votes
2 answers

Partial update of JSON Object in MySQL

Good afternoon, When I try to update part of a JSON Object, using ON DUPLICATE KEY UPDATE, how do you update a specific value with a key? The code executes successfully but all values are updated when I just want the stock to change on update. Any…
dimButTries
  • 661
  • 7
  • 15
7
votes
3 answers

How to null check MySQL JSON column property?

I'm working with MySQL 8.0.21. I need to write a query that works with the JSON column type. Some of the data inside the JSON documents have null values and I want to filter out these null values. Examples of possible rows, most properties in the…
Tyler
  • 957
  • 11
  • 27
7
votes
2 answers

Remove array element by value in mysql json

Is it possible to remove an element by its value (and not its index) in a json array in mysql? For example: # ['new', 'orange'] update waitinglist SET new = JSON_REMOVE("orange", '$') WHERE id=2; # now it will be ['new'] If not, is there a better…
David542
  • 104,438
  • 178
  • 489
  • 842
6
votes
2 answers

JSON_CONTAINS() with an array of JSON objects in MySQL

This is a sample database 'test' with a JSON column 'arr' containing an array of JSON objects +----+----------------------------------------------------------+ | id | arr …
Jayesh Padhiar
  • 73
  • 1
  • 1
  • 6
6
votes
1 answer

MySQL 5.7 : convert simple JSON_ARRAY to rows

I have a simple table with a JSON_ARRAY column like that: +----+---------+ | id | content | +----+---------+ | 1 | [3, 4] | | 2 | [5, 6] | +----+---------+ I want to list all the content references for a specific id SELECT JSON_EXTRACT(content,…
Jibeji
  • 453
  • 4
  • 14
6
votes
4 answers

Is it better to set the default MYSQL JSON value to `{}` or `NULL`

We are starting to use the MySQL json datatype. Is there any recommended best practices when storing default values as NULL or {} for the JSON datatype? What are the PROs and CONs for each?
Random5000
  • 1,562
  • 3
  • 16
  • 26
6
votes
4 answers

MySQL JSON - using IN statement | json_contains

I'm trying to select all columns where the roles property in the json column contains ANY of the values. Statements I've tried: SELECT * FROM components WHERE json->'$.roles' IN(1) this doesn't even work but it should in my opinion... SELECT *…
user7892649
6
votes
2 answers

MySQL 5.7.8 JSON merge new data

I'm trying to make a notes / comments system for an admin area with the new MySQL JSON support. Comments need to be editable and I wanted to add support for other things in the future, maybe file attachments (would store the filepath in JSON only…
Will Ormerod
  • 91
  • 1
  • 4
6
votes
1 answer

mySQL 5.7 return row as json using new json features

I was going over some of the new JSON features and was wondering if there is a clever (or obvious) way to return a rowset as a JSON object. Ideally without having to name the keys or use any kind of string manipulation. Example: TABLE: people id …
Sean256
  • 2,849
  • 4
  • 30
  • 39
5
votes
2 answers

Update a field with specific value inside a Json Object with MariaDB

I'm trying to update the data stored in a json column in MariaDB (libmysql version - 5.6.43 , Server: 10.3.34-MariaDB-cll-lve - MariaDB Server). My data is structured like this: ID json_data 1 {....} 2 {....} where json_data is…
Jean
  • 453
  • 4
  • 16
5
votes
5 answers

Ordering rows by JSON array column on MySQL & MariaDB

PostgreSQL allows rows to be sorted by arrays. It compares the first value of each array, then the second value and so on (fiddle): select array[2, 4] as "array" union select array[10] as "array" union select array[2, 3, 4] as "array" union select…
Jonas Staudenmeir
  • 24,815
  • 6
  • 63
  • 109
5
votes
1 answer

MySQL merge arrays from multiple JSON rows

I have a table with a json column that looks like this : +----+------------+ | id | myfield | +----+------------+ | 1 | ["1", "2"] | | 2 | ["3", "2"] | | 3 | ["2", "4"] | +----+------------+ How can I merge all values from myfield in one…
user345602
  • 578
  • 1
  • 12
  • 26
1
2
3
25 26