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
2
votes
2 answers

MySQL LIKE with json_extract

I have a MySQL query, where I filter by a json field: SELECT id, username FROM (SELECT id, Json_extract(payload, '$.username') AS username FROM table1) AS tmp WHERE username = 'userName1'; It returns 1 row, which…
Battle_Slug
  • 2,055
  • 1
  • 34
  • 60
2
votes
1 answer

Laravel eloquent update JSON column some fields without overriding everything else

We are trying to use JSON datatypes to store Profile dynamic data, and do not know all of the hard-coded field names to add to the fillable property in model. how to set (insert if the key not exists | update if the key exists) just one key, not all…
SoheilYou
  • 907
  • 5
  • 23
  • 43
2
votes
2 answers

mysql array of objects compare items

I have problem in mysql json that I want to compare one item of object in json array. example: in my table i have column aJson that is array : [{"Name":"Mohsen","Code":3},{"Name":"Ali","Code":5},{"Name":"Reza","Code":2}] I want to find the records…
Mohsen
  • 19
  • 4
2
votes
2 answers

Why don't MEMBER OF() or JSON_CONTAINS() use the multi-value index?

I have the following table with a multi-value index set up on a JSON integer array: CREATE TABLE test ( id INT UNSIGNED NOT NULL AUTO_INCREMENT, catIds JSON NOT NULL, PRIMARY KEY (id), KEY test_categories ((CAST(catIds AS UNSIGNED…
BenMorel
  • 34,448
  • 50
  • 182
  • 322
2
votes
1 answer

How to store a key and value in a table from a JSON object in MySQL

I'm having a MySQL database tables namely ds_message and ds_params, it table ds_message contains a JSON object in each row. I would like to store the key and value of a JSON object into the table ds_params for all the records by referring the…
B.Balamanigandan
  • 4,713
  • 11
  • 68
  • 130
2
votes
1 answer

Find and Replace a String usng a Json Key Value pair object in MySQL

I'm having a MySQL database table namely ds_message, it contains the template and JSON object. I would like to find the Key which is present in the template string with the JSON Key and replace the key with the JSON…
B.Balamanigandan
  • 4,713
  • 11
  • 68
  • 130
2
votes
0 answers

Sorted json array field

Is there a function in mysql to sort a json field? SELECT new FROM mytable ["orange", "apple"] ==> Sorting the json array SELECT SORTED(new) FROM mytable ["apple", "orange"]
David542
  • 104,438
  • 178
  • 489
  • 842
2
votes
1 answer

Select Objects from Array of Objects that match a property in MYSQL JSON

I have a table with 1 JSON type column city in a MySQL database that stores a JSON array of city objects with following structure: { "cities": [ { "id": 1, "name": "Mumbai", "countryID":…
Shyam Singh
  • 123
  • 2
  • 14
2
votes
2 answers

How to update nested JSON data type in MySQL 5.7

Using JSON_SET I have no problem updating simple JSON datatype, but with nested it just doesn't work. How query supposed to look like with format like this: { "textures":[ { "label":"test", "types":{ …
JayJay
  • 70
  • 1
  • 9
2
votes
1 answer

MySQL/MariaDB JSON_EXTRACT and JSON_CONTAINS

I have a table with the following structure: select * from test_table; id |load_balancer_name |listener_descriptions …
okelet
  • 716
  • 1
  • 8
  • 23
2
votes
2 answers

A use case for MySql JSON datatype

I am creating a DB-schema for a website on which users can write the Articles. I was almost done with the design and suddenly I read few blogs on JSON datatype in MySQL. As per blogs, there are certain use cases where JSON can be used: for storing…
The Coder
  • 3,447
  • 7
  • 46
  • 81
2
votes
3 answers

mysql date comparison using BETWEEN in MySQL JSON data

I'm struggling to compare dates which are stored in JSON data in MySQL. The format of the JSON would be something like {"0": {"end_date": "2018/06/30", "start_date": "2018/06/01"}, "1": {"end_date": "2018/06/30", "start_date": "2018/06/01"}} So,…
Tarun Parswani
  • 4,565
  • 3
  • 13
  • 13
2
votes
2 answers

MySQL JSON column add new array as element

I have a table with JSON type column, I want to update a column with new array element in existing JSON. What needs to be done: add an array in JSON column when employee punch_in and add another array in JSON column when employee…
Rahul Dadhich
  • 1,213
  • 19
  • 32
2
votes
1 answer

PDO-MySQL: Boolean values get converted to 1 or empty string on prepared statement binding

I'm trying to insert some boolean values into JSON-type columns. $taskSql = "INSERT INTO Tasks (data, taskListId) VALUES (JSON_OBJECT('title', :title, 'done', :done), :taskListId)"; $taskStatement =…
Philippe
  • 1,715
  • 4
  • 25
  • 49
2
votes
1 answer

Path syntax to perform operations on a MySQL JSON object with an empty key

Given the following JSON object used in MySQL 5.7 or later: {"": 1} What is the valid path syntax to extract the value from the JSON object with an empty string key? select JSON_EXTRACT('{"":1}', '$.""'); The above SQL is the best of my…
avelis
  • 1,143
  • 1
  • 9
  • 18