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

MySQL - How to find number of occurance in JSON?

I have following table - +-----+-----------+--------------------------------------+ | id | client_id | service_values | +-----+-----------+------------+-------------------------+ | 100 | 1000 | {"1": "60", "2": "64", "3":…
ktech
  • 3
  • 2
0
votes
1 answer

How Can I merge JSON ArrayList in formated String Query?

I Have this JSON colunm in my MYSQL (one row): [{"motivo": {"id": 60}, "documento": {"id": 3}, "motivoStr": "Comprovante de Endereco"}, {"motivo": {"id": 61}, "documento": {"id": 4}, "motivoStr": "Comprovante de Renda"}] I need format this in mysql…
0
votes
1 answer

How to use JSON_REMOVE with several rows from a subquery?

I have a table with json-column in the column data and a table with keys to delete from the first table. MySQL 5.7. id data 1 {"key1": "value1", "key2": "value2", "key10": "value10", "key100": "value100"} 20 {"key1": "value1", "key18":…
0
votes
1 answer

How to combine 'NESTED PATH` values using JSON_TABLE to fetch a nested JSON array?

I have a Json array while looks as below [{"keyId": "aded5b0107bb5a936604bcb6f", "keyNames": ["abc1, abc2"], "keyDesc": "test"}] I am using JSON_TABLE to fetch all the values and I have written the following query: SELECT j.keyId, j.keyNames,…
meallhour
  • 13,921
  • 21
  • 60
  • 117
0
votes
1 answer

MYSQL query to get array list with each row having different children

Lets say we have three tables category_level_one id cat_name 1 Physics 2 Chemistry category_level_two id sub_cat_name cat_l1_id 1 Organic 2 2 Inorganic 2 3 Physical 2 4 Mechanics 1 5 Electricity and…
Mr X
  • 1,637
  • 3
  • 29
  • 55
0
votes
2 answers

search between two iso date in JSON mysql

Below is my JSON which is save in response column of mysql table { "data": { "submit": false, "lastName": "abc", "firstName": "xys" }, "metadata": {}, "createDate": "2021-04-23T00:00:00+00:00" } Now I want to…
silentcoder
  • 992
  • 3
  • 9
  • 21
0
votes
2 answers

Using JSON_ARRAY_APPEND to insert large array into JSON document

I am using JSON_ARRAY_APPEND to update an array object in an existing entry in the table. I am doing it via code and query looks as below: UPDATE table t1 SET t1.value = JSON_ARRAY_APPEND('[]', '$', JSON_OBJECT('desc', '${desc}', …
meallhour
  • 13,921
  • 21
  • 60
  • 117
0
votes
1 answer

Sorting a JSON object in SQL

I have a SQL variable which stores the following JSON object: {"name": "Dan", "country": "Japan", "job": "manager"} My goal is to sort the object based on the keys alphabetically and return it {"country": "Japan", "job": "manager", "name": "Dan"}.…
0
votes
1 answer

JSON_ARRAY_APPEND not inserting json document in MySQL

I have started using MySQL 8 and trying to insert JSON data type in a mysql table My table t1 looks as below: # id type jval 1100000 type1 I want to insert value for jval which is of type JSON. UPDATE t1 mdata SET mdata.jval =…
meallhour
  • 13,921
  • 21
  • 60
  • 117
0
votes
1 answer

How to perform add, update and delete on this complex json data type field in MySQL?

I am totally new to JSON data type in MySQL. I installed MySQL 5.7.34 a few days ago and now I'm playing around with it. So, I have following table in MySQL 5.7.34: CREATE TABLE tb_products ( product_id int(11) NOT NULL AUTO_INCREMENT, …
Sachin
  • 1,646
  • 3
  • 22
  • 59
0
votes
1 answer

Mysql - JSON_CONTAINS find within array

I have json column with following data: [{"option_id": 1, "category_id": 1}, {"option_id": 2, "category_id": 2}] I am trying to find records with option_id = 1 This is query I am trying: select count(*) as aggregate from `complaint_forms` where…
Sasha
  • 8,521
  • 23
  • 91
  • 174
0
votes
1 answer

MYSQL Search data from json field where datas position might differ

I have table where i have to find ID when i only know for example dcid:xxxxxx which can be found in different places in IDENTIFIERS column +----ID----+--------------------------------IDENTIFIERS-----------------------------------+ | 234512 |…
LynXXi
  • 1
0
votes
0 answers

Select rows from a MySQL JSON column that meets a condition

I am trying to select rows from a JSON table column (in MySQL 5.7.3) that meet a certain condition. And yes, using a JSON column rather than just creating a separate MySQL table is intentional. Consider the following MySQL table: Table:…
Gijs
  • 165
  • 1
  • 9
0
votes
1 answer

MySQL Convert JSON column to array and get pages

It's pretty difficult to pick a title, I'm sorry for that! What I have for a database structure is the following columns: pages id (int) url (string) content (string) created_at (timestamp) groups id (int) related_page_ids (json) domain_id…
Donny van V
  • 921
  • 1
  • 10
  • 22
0
votes
2 answers

How to make filtering and sorting JSON data in MySQL faster?

I have table "listing_data" which is about 150.000 rows. I have problem with filtering and sorting when the data comes from JSON. I have added keys on each column which needed for filtering and sorting. However, as one column "data" used to store…
Raden Bagus
  • 314
  • 4
  • 14