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

MariaDB 10.2 Query with JSON

I have some trouble with my query; SELECT *, JSON_VALUE(cms_routing_data, "$.cms_routing_date.field") AS order_row FROM database.cms_routing WHERE cms_routing_module = 'events' AND cms_routing_data != '' AND order_row >= '2018-05-11' ORDER BY…
J Quest
  • 593
  • 2
  • 17
0
votes
0 answers

MySQL 8: JSON_TABLE not working

In MySQL 8.0.4 and later, there is a function named "JSON_TABLE". I tried the examples from the official mysql docs, but JSON_TABLE returned an error: SELECT * FROM JSON_TABLE( '[{"x":2,"y":"8"},{"x":"3","y":"7"},{"x":"4","y":6}]', "$[*]"…
user2458995
  • 161
  • 2
  • 3
0
votes
1 answer

Nested update query in JSON data type column in mysql 5.7

I have added a new JSON Data type column (bill_plans)in my table . Now I want to update the bill_plans column like this [{ "cycle": 1, "fee": 1000}, { "cycle": 3, "fee": 2800}, { "cycle": 10, "fee": 10000} ] I was successful in creating the column…
vicks
  • 13
  • 4
0
votes
0 answers

Between operator is not working in JSON datatype - mysql?

I have following JSON format in the mysql table statistics and the column is stats { "stats":{ "cities":[ { "name":"Melbourne", "country":"AU", "value":0.245 }, { …
karthick
  • 5,998
  • 12
  • 52
  • 90
0
votes
1 answer

How to filter two properties in the JSON - mysql?

I have following JSON format in the mysql table statistics and the column is stats { "stats":{ "gender":{ "male":40, //40 is percentage "female":50 }, "cities":[ { …
karthick
  • 5,998
  • 12
  • 52
  • 90
0
votes
0 answers

MySQL JSON_Array receiver type in Java

I have MySql query that will return as JSON_Array from MySql DB. Query:- SELECT id, JSON_ARRAY(GROUP_CONCAT(address)) FROM tb_address GROUP BY id; But I can't find proper variable type to receive that return value in Java. Someone can share…
JohnC
  • 209
  • 4
  • 16
0
votes
1 answer

mysql can we change maximum default JSON column length without changing maximum packet size?

I read here that max allowed json in 1 GB. Can we somehow change this default behaviour to reduce this MAX limit to few MBs or any other value in particular table column with json datatype?
Sahil Sharma
  • 3,847
  • 6
  • 48
  • 98
0
votes
1 answer

MySQL JSON - SELECT WHERE

I'm trying to SELECT objects base on the roles property values. Example: Select all names where role is 1 //response would return danny Query Statement: SELECT JSON_EXTRACT(username,'$[*].name') FROM objects WHERE…
user7892649
0
votes
0 answers

Mysql GROUP_CONCAT skips part of string

I am trying to combine JSON datatype columns in a grouped mysql result with GROUP_CONCAT but at character 5001 i get this weird missing piece of json. { "IMO": "XX", "LAT": "52.093330", "LON": "3.587667", "DSRC": "TER", "MMSI":…
Han Dijk
  • 1,602
  • 1
  • 14
  • 20
0
votes
1 answer

MYSQL JSON_MERGE and Group By

I have the following table of data What I am trying to do is GROUP BY timestamp and JSON_MERGE the objects. What I expect as a result is the following. I expected the following query to work but I just get an error SELECT timestamp,…
Cathal Coffey
  • 1,105
  • 2
  • 20
  • 35
0
votes
0 answers

Searching inside JSON array in MySQL

I have a table of Products that also contains JSON datatype with JSON array - json_array() field. For the example "Products" table includes the following: ProductID ProductName ProductDesc CreateDate Price HistoricalPrices (json) I am appending…
Dror
  • 1,262
  • 3
  • 21
  • 34
0
votes
1 answer

How to implement json datatype to store price history

I have a "Products" table in MySQL (5.7.10) that contains many products. I want to include into "Products" table a field called HistoricalPrices using JSON datatype. For the example "Products" table includes the…
Dror
  • 1,262
  • 3
  • 21
  • 34
0
votes
1 answer

MySQL JSON datatype to store historical price

I am thinking about possible solution to save historical data prices in MySQL 5.7+ with JSON datatype instead of adding new row per historical price. Case description: I have a table of products current prices and product description called…
Dror
  • 1,262
  • 3
  • 21
  • 34
-1
votes
0 answers

MySQL8.0 slightly slower than MySQL5.7 when query entries with JSON

When I execute the same select clause on the same table both in MySQL8.0 and MySQL5.7, I find the execution time is not what I expect. The same query clause takes more time in 8.0 than that in 5.7 A simple table contains a json type and other simple…
PatchyTIS
  • 1
  • 1
-1
votes
1 answer

How to retrieve JSON Object data MySQL select query

I Have one table i.e discount_details. I have 2 columns are the ID , DiscountTotal. I want to sum the each record line total json object with in the D array (64,10,10 are the 1st record values) . Like here 64,10,10 are the values,need to sum total…
1 2 3
25
26