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
1
vote
2 answers

Mariadb JSON_EXTRACT with condition on Json object

How do I extract only value based on "id" = "BAR" +------------------------------+ | data | +------------------------------+ | {"id": "FOO", "code": "FOO"} | | {"id": "BAR", "code": "BAR"}…
rosia
  • 219
  • 1
  • 2
  • 6
1
vote
1 answer

Category and subcategory recursive query SQL

I got a little problem with my recursive query. I got a database of menu of a bar. We got: Category, each category got sub-categories and each-subcategories got multiple items. The database is this one and the query is linked inside: CREATE TABLE…
1
vote
1 answer

Mysql Json extract with conditional filter

I'm trying to query some json data through a filter. Given a json array like this: [ {name:'name1', country:[{name:'France', people:10}, {name:'Japan',people:20}]}, {name:'name2', country:[{name:'France', people:20}, {name:'Japan',people:40}]}] I'd…
1
vote
1 answer

MySQL select rows where the JSON array column is a subset of another array

Hy, I have a table of machines with a JSON array column called cells. Some example data: machine_id | cells ----------------------------------------- 1 | ['TE001', 'GM0002'] 2 | ['TE001', 'TK123', 'AW123'] 3 |…
gbalduzzi
  • 9,356
  • 28
  • 58
1
vote
1 answer

mysql update json attribute and another column in one query

I need to update a json value in a column as well as update another column in the same query. Something like this: UPDATE fixtures SET jsonResults = '{}', JSON_SET(jsonFixture, '$.time_status', '0') WHERE intRefID = 88961323; How can I…
Justin Wade
  • 127
  • 10
1
vote
2 answers

Query mysql json column array using AND NOT CONTAINS (mysql 5.7)

I have a table with a json column that contains an array of objects, like the following: create table test_json (json_id int not null primary key, json_data json not null) select 1 as json_id, '[{"category":"circle"},{"category":"square",…
Regular User
  • 682
  • 7
  • 16
1
vote
0 answers

Use LIKE statement in a json type column

I have a table with some columns arraged kinda like this: Column a (type: INT) Column b (type: TINYINT) Column c (type: INT) Column d (type: TEXT) Column e (type: JSON) I want to retrieve columns a b and c where the column e has the value x…
Lysander12 PT
  • 154
  • 16
1
vote
2 answers

MySQL JSON_SEARCH for multiple arguments of nested objects

I have a db-table containing json formated strings: CREATE TABLE `template` ( `Id` int(10) unsigned NOT NULL AUTO_INCREMENT, `TemplateData` longtext NOT NULL, PRIMARY KEY (`Id`) ); INSERT INTO template (Id, TemplateData) VALUES (1, '[]'), (2,…
MIB
  • 337
  • 2
  • 15
1
vote
0 answers

Difference between two JSON fields in mysql?

I have a table consists of 3 JSON fields and each of them has data in the format like [1,2,3,4]. I want to find the difference (the elements which are in one field that is not in another field) between any of the two JSON fields using MySQL query…
1
vote
1 answer

Query a JSON column to extract the selected map in Array List of Mysql 5.7

I'm using Mysql 5.7 and it has following data set. mysql> select id, browser from events; +----+--------------------------------------------------------------------+ | id | browser …
Parinda Rajapaksha
  • 2,963
  • 1
  • 36
  • 40
1
vote
2 answers

MySQL: How to use JSON_ARRAY in WHERE..IN clause?

Is there a way to use the JSON_ARRAY in WHERE..IN clause? For example, the following is the JSON_ARRAY. SET @codes := JSON_ARRAY('CA' , 'NY' , 'TX'); And I need to do something like the following: SELECT * FROM `state` WHERE `code` IN (@codes);
Manoj Shrestha
  • 4,246
  • 5
  • 47
  • 67
1
vote
1 answer

Select all rows where key in JSON key value array

Okay, so I just found out that you can save and select on the JSON data type in MySQL. I tried to follow user2458995's answer to a similar question as mine, (The difference is mine is with a key and the value is an array) I copied his exact code,…
ii iml0sto1
  • 1,654
  • 19
  • 37
1
vote
2 answers

WHERE x IN works with a single value, not with multiple on json

There's a hard to understand issue with querying on a json field in MySQL. The data column is of type json. The following query works perfectly fine SELECT * FROM `someTable` WHERE data->'$.someData' in ('A') However the following one returns…
Return-1
  • 2,329
  • 3
  • 21
  • 56
1
vote
1 answer

MySQL JSON Data Need SQL Query to Sort Array of Objects Order based on key value in object

I have a table named posts with columns id, title, tags. CREATE TABLE IF NOT EXISTS `posts` ( `id` int(6) unsigned NOT NULL, `title` varchar(100) NOT NULL, `tags` json NOT NULL, PRIMARY KEY (`id`) ) DEFAULT CHARSET=utf8; Stores data…
dagalti
  • 1,868
  • 1
  • 13
  • 15
1
vote
2 answers

JSON Elements to Row OR JSON to table Row in MYSQL(Export JSON in MySQL)

We could have many request to store json to database. sometime we need to convert JSON Key set to Table - Rows. How to extract complete json in Mysql table? i.e. Having below json and requirement is to store each set as a row in the table. '{ "log":…
JERRY
  • 1,165
  • 1
  • 8
  • 22