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
2
votes
1 answer

MySQL: update specific property value of a JSON object in a JSON Array

I have MySQL DB with a table that has a JSON column. The sample data in the columns looks as below: [{"id": 1, "value": 23.4}, {"id": 2, "value": 54.3}, {"id": 3, "value": 4.33}] I wanted to update the value property of all the objects in the…
vamsi
  • 23
  • 3
2
votes
1 answer

Search an array of objects for specific key having a certain value

I want to retrieve all rows from a table in MySQL table where there is a JSON column called items with content like…
Chargui Taieb
  • 75
  • 1
  • 10
2
votes
1 answer

How do I update a json column with a subquery?

I have MySQL 5.7 and table1 with json column data. SELECT id, data FROM table1; id data 1 {'key3': 'value3'} 2 {'key5': 'value5'} I can use: UPDATE `table1` as `t1` SET `t1`.`data` = JSON_MERGE_PATCH(`t1`.`data`, JSON_OBJECT('key1',…
2
votes
1 answer

MySQL 5.7 JSON_REMOVE multiple values from array in one query

I have a MySQL 5.7 database with a JSON column containing an array of strings. I need to remove a varying number of those strings from the array, by value, in one query. Example JSON: ["1-1-2", "1-1-3", "1-2-2", "1-2-3", "1-1-16", "1-1-17"] I may…
jessica
  • 3,051
  • 2
  • 30
  • 31
2
votes
1 answer

Modify property in a JSON array of objects

Let's say I have a JSON column comments in a table posts, where I store an array of objects with 2 properties : at with the datetime of creation and content as the comment's content. I'd like to replace the content of every comments by a static…
AlterPHP
  • 12,667
  • 5
  • 49
  • 54
2
votes
1 answer

Find row in table depending on JSON-Object key-value

I have a MySQL database with a table called users. This table has some columns. In a column called actions there are JSON objects stored. For example: { "name": "joe", "tasks": [ { "id": "1", "type": "wait", "value":…
Battalgazi
  • 367
  • 1
  • 4
  • 20
2
votes
1 answer

Update a nested value from a Json field

Consider this table: DROP TABLE IF EXISTS `example`; CREATE TABLE `example` ( `id` int NOT NULL AUTO_INCREMENT, `content` json NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3; And these rows: INSERT INTO example(content)…
Chuck
  • 98
  • 2
  • 10
2
votes
2 answers

Laravel Query - Extract specific field in aray of objects in A JSON column and calculate

I would like to get only a specific field in a json column and calculate its values. The json column is an array of objects [ { "sku": "DT-ALD3-EA18", "adId": 244077676726655, "asin": "B07V3SSLN1", …
rai
  • 197
  • 2
  • 14
2
votes
2 answers

Mysql: get json array length and ignore null values

I have this query select json_length(data->"$.row.*.code") as count from hospitalization_history where id = 238 The result in count is 8, because data->"$.row.*.code" returns ["J00.00", "V01.00", "G00.00", null, null, null, null, null]; How can I a…
Andriy Lozynskiy
  • 2,444
  • 2
  • 17
  • 35
2
votes
0 answers

MySQL JSON query ERROR "Missing a closing quotation mark in string"

Edit: Just realized I left out the closing quote here: {"area": "jp", "type": "female, "tag": {"411": "Doo Wop"}}, I mistyped it in the original question but this quote is actually included in db column, I apologize for misunderstanding caused by…
oeter
  • 627
  • 2
  • 8
  • 23
2
votes
1 answer

MySQL 8: Subquery using JSON_TABLE not working unless subquery result is manually entered

Here is a minimum working edition of the problem I am facing. Following the JSON table example on the documentation page, I have the following table test in MySQL 8, where id is an INT field and data is a JSON field. id | …
Cogicero
  • 1,514
  • 2
  • 17
  • 36
2
votes
3 answers

How to get related items by id inside of json integer array column in MySQL

My goal is how to get a relation between a column that have references in a json array to other one. In a simplified way, I have two tables: table_a | id | references | |-----|------------| | 1 | "[1,3]" | | 2 | "[2,3]" | Whose…
Juan Antonio
  • 2,451
  • 3
  • 24
  • 34
2
votes
1 answer

MySQL JSON_REMOVE with multi array

I have the following JSON doc stored in MySQL 8 column called my_data: { "managers": { "11": { "created": "2019-11-07" }, "12": { "created": "2020-03-12" }, } } How can I remove the "11" info. UPDATE table SET…
kylex
  • 14,178
  • 33
  • 114
  • 175
2
votes
1 answer

How to use JSON_EXTRACT to get values of a string index

I have the followed data rows: I'm trying to use JSON_EXTRACT to get rows only if inside jot_locale_vars has index equals "2". SELECT jot.*, (JSON_EXTRACT(`jot_locale_vars`, '$[2]')) as localeVar FROM job_type jot WHERE…
Maykel Esser
  • 289
  • 3
  • 15
2
votes
1 answer

MySQL: save JSON data via stored procedure in Node.js

I am trying to save some JSON data with a stored procedure in MySQL and Node. I have post, post_tag and tag tables. DROP TABLE IF EXISTS post_tag; DROP TABLE IF EXISTS post; DROP TABLE IF EXISTS tag; DROP PROCEDURE IF EXISTS select_all_posts; DROP…
Emille C.
  • 562
  • 1
  • 7
  • 23