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 - Select value of JSON array based on the value of an element

I have a TEXT variable in MySQL 5.7 that stores a JSON. Within this JSON there is a list with the following structure of N elements: "brandedFares": [ { "type": "name1", "status": "allow" }, { "type": "name2", "status": "not allowed" }, ... The…
WillDCC
  • 11
  • 2
1
vote
1 answer

How to populate a JSON path dynamically in MySQL using only SQL syntax

I'm trying to write a SELECT statement that dynamically populates a JSON path based on values from a metadata table. I have two tables: a table of metadata describing the JSON structure, and a table of business data including each employee's name…
Chris
  • 103
  • 6
1
vote
1 answer

Mysql JSON_OBJECT aggregate json objects to json array where id matches

As mentioned in the title, i've been trying to aggregate json objects to a json array where the id is the same. I've got a main table "kurs" that has a unique id and is connected to the table "kompetenz" over a link table. For each "kurs" there can…
VSWizzard
  • 75
  • 4
1
vote
1 answer

How to select a nested item of json datatype?

I have a MySQL table named computers with a column named details that is json data type formatted. I've inserted a value like this in that column: '{ "name": "Chrome", "os": "Windows", "resolution": { "x": 1680, "y": 1050 } }' I can simple get…
Martin AJ
  • 6,261
  • 8
  • 53
  • 111
1
vote
2 answers

How much is too much when storing JSON in MySQL

I have wondered about my current database design I made for my uni organization website. In general this website is for displaying informations and events. The event is different for each departement. It does some normal things like displaying…
newtocoding
  • 97
  • 2
  • 5
1
vote
1 answer

MySQL find in database where value in array in JSON is BETWEEN something

I have database user info 0 {"messages": [{"user_to": 1, "timestamp": 1663000000}, {"user_to": 2, "timestamp": 1662000000}]} 1 {"messages": [{"user_to": 0, "timestamp": 1661000000}, {"user_to": 2, "timestamp":…
1
vote
1 answer

Query to remove duplicated Json Objects from a Json Object Array in MySQL

{ "tr_no": "2", "actions": [ { "link_type": "1" "tr_link": "www.abc.com" }, { "link_type": "1" "tr_link": "www.def.com" }, { …
xcxc0
  • 99
  • 1
  • 9
1
vote
1 answer

how to extract json keys and values as table with 2 columns mysql 5.7

Having this in column named "value" on table named "test" with varchar data type: '{"3": "3", "2": "7", "6": "1", "1": "2", "5": "5"}' the output I need: col1 col2 3 3 2 7 6 1 1 2 5 5 I'm having difficulties to…
1
vote
1 answer

Using JSON_TABLE to convert List into Rows

I have a database column (named "product_parents") formatted as a JSON object that contains the following data: '["A", "B", "G", "H", "C", "E", "P", "R"]' I want to use JSON_Table to create separate rows for each item. Ideally I would get something…
1
vote
1 answer

Is there a possibility to find member of in two array containing "ANY" not "ALL" mysql json

Im trying to search an array within an existing one in MySQL for contains check, with the condition that even if a single element is intersecting result is true. Tried existing functions - JSON_CONTAINS / MEMBER OF. But they seem to check "ALL"…
Bhuvan Rawal
  • 386
  • 4
  • 15
1
vote
1 answer

mysql - sum of json elements, group by and select all

I have the following table structure and data: DumpTime ProcId NodeId Stats 2022-07-07 13:03:03 9 NODE_IDD02 {"First":1,"Second":5,"Third":2} 2022-07-07 13:17:35 8 NODE_IDD03 {"First":2,"Second":3,"Third":4} 2022-07-07…
1
vote
1 answer

select distinct values from json arrays in mysql database

I have table like this my_table id my_json 1 ['1','2','3'] 2 ['2'] 3 ['2','3'] ... 12000 .... I want to find all distinct values in json's arrays like this result '1' '2' '3' I have this code but i need split values to rows set…
1
vote
2 answers

join json array as a string in MySQL

I have these rows in mysql table. tags is a json type column and json array is stored. id tags 1 ["a", "b"] 2 ["a", "b", "c"] 3 [] I want to return join value of the array elements. i.e.) id tags 1 "a, b" 2 "a, b, c" 3 "" Is there…
user16012143
  • 139
  • 7
1
vote
1 answer

MySQL Search within a JSON Array from a Select Statement

I am new to JSON in MySQL. I am using MySQL 5.7. I am attempting to search within a JSON Array for values from another field. Example. CREATE TABLE t1 (jID INT UNSIGNED NOT NULL AUTO_INCREMENT, jdoc JSON, PRIMARY KEY(`Jid`)); CREATE TABLE t2 (ID…
1
vote
1 answer

How to Update an element from a JSON Array based on 2 conditions in Mysql 8.0.26

I have the following json data in a mysql column: { "components":[ { "url":"www.google.com", "order":3, "accountId":"123", "autoPilot":true, }, { "url":"www.youtube.com", …
Michael Bat
  • 101
  • 9