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

MySQL JSON: How to find object from key value

I'm trying to find a way to search a JSON object and get a particular key but search on another key. Here is an example schema: CREATE TABLE `fields` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `label` varchar(64) COLLATE utf8mb4_unicode_ci…
John Mellor
  • 2,351
  • 8
  • 45
  • 79
3
votes
1 answer

Sqlalchemy use json_set to update specific JSON field

I intend to use func function to update a specific JSON field in Sqlalchemy, but I get some problem, here is my code to update field: self.db.query(TestModel).filter(TestModel.test_id == self._test_id).update( {field_name: func.json_set( …
Gary
  • 1,199
  • 1
  • 9
  • 23
3
votes
1 answer

MYSQL JSON_SET adds backslash

I want to update JSON array in the database I'v got this array: Array ( [id] => 34 [var1] => val1 [var2] => val2 [var3] => val3 ) and I need to update json by the id query…
3
votes
1 answer

Read MySQL JSON column array as rows

I have a table X - id - name (eg: 'testing') - array_column_name (eg: ["value1","value2"]) I want a query that will return the following output. _____________________ | column1 | column2 | | testing | value1 | | testing | value2 …
user9801251
3
votes
1 answer

Get path expression of a value inside JSON data using case insensitive search using MySQL Json_search function

I have a requirement where by I need to extract JSON path expression from a JSON data searching by a value. For example if I have the JSON data stored like SET @j = '[{"name":"Kiran Muralee", "age" : 30 , "Salary" : 30000}, {"Name":"Arun…
Kiran Muralee
  • 2,068
  • 2
  • 18
  • 25
3
votes
2 answers

Find in json by key in mysql

I have a json and i want to extract the value of a key. I know the key name but not the location. My json is complex and would look like this in mysql: set @jsonstr:=' { "glossary": { "title": "example glossary", "GlossDiv": { …
ankur
  • 557
  • 1
  • 10
  • 37
3
votes
1 answer

MySQL: Selecting boolean value from JSON field

I'm using MySQL 5.7.19 I'm trying to retrieve a class that looks as follows class Task { public $title; public $done; } the title and done properties are saved in a JSON column. The code that I use to retrieve looks as follows: …
Philippe
  • 1,715
  • 4
  • 25
  • 49
3
votes
2 answers

Correct syntax for mysql JSON path to traverse arrays?

My question is about searching through the contents of json arrays when searching in mysql's JSON data type. DB Structure So, if i have two rows in a mysql table, with a json field, called foo. The first row has: { "items": [ {"type": "bar"} …
stef
  • 1,446
  • 1
  • 17
  • 26
3
votes
1 answer

How much can a MySQL 5.7 JSON datatype can hold

How much value / size can a MySQL 5.7 JSON datatype hold? What are the limitations of the MySQL 5.7 JSON datatype?
AMALRAJ K V
  • 63
  • 1
  • 5
2
votes
1 answer

MySQL 8.0.33 error when selecting json column : Out of sort memory, consider increasing server sort buffer size

I have a table with a JSON column. I want to select a specific data within the JSON column. My query was working fine with MySQL 5.7 (Ubuntu 16.04), but does not work with MySQL 8.0.33 (Ubuntu 22.04) : Query : select…
Maxime Freschard
  • 1,066
  • 2
  • 15
  • 26
2
votes
1 answer

Return all keys with their array items in a SELECT on a JSON field in MYSQL 8

Good guys I have a table in which a field is json in the following format: { "monday": [{"end": "18:00:00", "start": "09:00:00"}, {"end": "21:00:00", "start": "19:00:01"}], "sunday": [{"end": "15:00:00", "start": "14:00:00"}], "saturday":…
2
votes
1 answer

How to update json data in a column using mysql

My goal is to completely change the listings field Structure in the database: { "other":{ "price": 0, "isFrozen": false, }, "listings": { "price": 0, "isFrozen": false, "isLocked": false, "pricings": [], "isSalable": false, "listingId":…
kaann.gunerr
  • 170
  • 1
  • 13
2
votes
2 answers

How to get a specific object in an JSON array in MySQL?

I have a JSON column "jobs" that looks like this: [ { "id": "1", "done": "100", "target": "100", "startDate": "123123132", "lastAction": "123123132", "status": "0" }, { "id": "2", "done":…
2
votes
0 answers

How do I achieve Json Array in Json Object in MariaDB?

I have a table Named `FAMPRODUCT` and it's somewhat like this. Select * from FAMPRODUCT; Pid|Prd_Code|Prd_Name|Prd_SrNo|Prd_Rate|Stock| 1| 1001| Pr1| 1| 40.000| 50| 2| 1001| Pr1| 2| 50.000| 40| 3| 1001| …
bonny
  • 688
  • 1
  • 14
  • 33
2
votes
2 answers

Concatenate JSON array to string

I have the following result, which are JSON arrays: ["elative degree of كَبِير‎ (kabīr):", "greater; greatest"] ["elative degree of كَبِير‎ (kabīr):", "bigger, larger; biggest, largest"] ["elative degree of كَبِير‎ (kabīr):", "older, elder; oldest,…
Kohjah Breese
  • 4,008
  • 6
  • 32
  • 48