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
0 answers

MySQL JSON to GROUP_CONCAT

I have a JSON data in MySQL like this [["D01","DATA 1"],["D02","DATA 2"],["D03","DATA 3"],["D04","DATA 4"],["D05","DATA 5"],["D06","DATA 6"]] and I need it to format like this using GROUP_CONCAT D01~D01~D03~D04~D05~D06 So far I have query like…
juntapao
  • 407
  • 3
  • 12
1
vote
1 answer

Storing data in JSON vs MYSQL row

I am coding a small app and have a question. i am deciding if storing data in JSON or mysql row is best for my scenario. I am coding an app that may have lots of page hits, and because of that, i am thinking to store JSON encoded array into a column…
user3620142
  • 135
  • 2
  • 3
  • 13
1
vote
2 answers

Remove elements from string json array in MySQL

I have table with many columns that contains string array json of objects. I need to remove several elements from these arrays. I find how can I remove elements from one row, but how can I do it on multiple rows? For one row I used json_search to…
Squeez
  • 919
  • 2
  • 12
  • 30
1
vote
1 answer

SQL JSON Column (IgnoreCase)

I have a SQL column in JSON Format which looks like data->'$.name' and I wanted to ask if it's possible to make an Ignore Case to this column like UPPER(data->'$.name'). Example: select * from TYPE_hugo_DATA WHERE UPPER(data->'$.name') =…
Markus G.
  • 1,620
  • 2
  • 25
  • 49
1
vote
2 answers

Query a JSON array in Mysql

I have the following JSON object. SET @j = '{"id": [1, 2, 4]}' I'm trying to query the JSON object to find if the id contain 2 in it. select JSON_CONTAINS(@j,'one','%2') I don't think the above JSON_CONTAINS are correct but I'm not sure what would…
Viren
  • 5,812
  • 6
  • 45
  • 98
1
vote
1 answer

How to use ransack to search MySQL JSON array in Rails 5

Rails 5 now support native JSON data type in MySQL, so if I have a column data that contains an array: ["a", "b", "c"], and I want to search if this column contains values, so basically I would like to have something like: data_json_cont: ["b"]. So…
user1011792
0
votes
0 answers

How to extract Nested Json Keys in Mariadb?

I have this json file { "data": { "Header": { "num": 1000095371, "name": "1000095371 LE" }, "character": { "b1234": { "ID": 1 }, "b1256": { "ID": 2 }, "b12389": { …
Bala
  • 1
  • 1
0
votes
0 answers

How to sum by keys and all in json in MySQL?

My sample table be like: id data_json 1 {"1001":{"101": 2, "102": 3, "103": 4},"1002":{"101": 1, "102": 2, "103": 3}} i want result like this: sum_by_key sum {"101": 3, "102": 5, "103": 7} 15 this my code: SELECT …
gdg tolis
  • 53
  • 5
0
votes
0 answers

How to update JSON value in mysql database using X Dev API for relational table

I am trying to emulate UPDATE user SET userDetails = JSON_SET(userDetails, '$.name', 'Alok') WHERE id = 1; to update JSON value using X Dev API. var updateValue = `JSON_SET(userDetails, '$.name', 'Alok')` var result = await…
Alok
  • 7,734
  • 8
  • 55
  • 100
0
votes
1 answer

Comparing two datetimes in MySQL JSON

I have a MySQL table called "contract" with a JSON-type column. The data in this column looks like this: {"5": "1663966800"} This property is a UTC time in seconds, stored as text. I am looking to query the table to see all entries where this…
Boris K
  • 3,442
  • 9
  • 48
  • 87
0
votes
1 answer

How to retrieve matched data from mysql table text datatype?

I have one query to build to get matched data from table having value as text in json format. CREATE TABLE `post_sender_detail` ( `version_id` int(10) unsigned NOT NULL, `subject` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT…
rkrathor
  • 77
  • 7
0
votes
1 answer

Search an array of JSON objects in MySQL

I am trying to retrieve an element from a JSON array in MySQL using JSON_SEARCH based on multiple properties ("fromUnit" and "toUnit"). The array is as follows: SET @unitConversions = '{ "unitConversions": [ { "fromUnit": "ounce", …
0
votes
1 answer

How do I optimize a MySQL Query which uses a function to parse JSON data

I have an Analytics table that has the following structure: +---------+----------+----------------+------------+---------------------+-------+ | user_id | label_id | application_id | session_id | date | score…
Dimitar
  • 1,148
  • 8
  • 29
0
votes
1 answer

OrderBy date in json column of table in laravel

One of the columns in my table is array_object. The input of this column is json. In this json there are two variables name and date. Now I want to sort the rows of this table based on the closest date from today onwards. It is important to note…
Mohsen Gorzin
  • 186
  • 1
  • 10
0
votes
3 answers

MYSQL Search JSON Values: how to search value from another table without hard coding index?

I have 2 tables in mysql database as followed: character_classes table: +--------+----------+ | id | name | +--------+----------+ | CLA001 | assassin | | CLA002 | knight | | CLA003 | vanguard | +--------+----------+ player_inventories…
free2idol1
  • 174
  • 1
  • 3
  • 12