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

Count how many rows have a nested value in the arrays of a json column in MariaDB 10.4

I have the following values ​​inside a cell of a json column in MariaDB 10.4: { "0": [ 21, "Pallet da 1250kg (50 * Sacco da 25kg)", "1250", "kg" ], "1": [ 21, "Sfuso", "12", …
Memmo
  • 298
  • 3
  • 8
  • 31
5
votes
1 answer

Rails has_many based on a json array column

Is it possible to tell Rails to use a JSON column as the "backend" for a relation? I have an Article model, that stores comment ids inside a JSON column Article#comment_ids: class Article < ApplicationRecord def comments Comment.where(id:…
23tux
  • 14,104
  • 15
  • 88
  • 187
5
votes
1 answer

how to push an element to a JSON in MySQL

I am trying to push a new element to a JSON array in MySQL. ex. in following table People: id(int) liked_ids(JSON) 1 [2,3] Now I want to push 5 into liked_ids. I had a look at JSON_ARRAY_APPEND but couldn't figure out a way to…
Ayan
  • 8,192
  • 4
  • 46
  • 51
5
votes
3 answers

How can I extract all values of a particular attribute within elements inside a JSON array in MySQL?

I've the below data stored in a column of MySQL table which is of "json" data type (MySQL v5.7.9) [{"id": "26", "title": "similar1", "score": "0.97"}, {"id": "27", "title": "similar2", "score": "0.87"}, {"id": "28", "title": "similar2", "score":…
NKP
  • 79
  • 9
5
votes
2 answers

mysqlimport issues "set @@character_set_database=binary" which prevents loading json values

I have been using mysqlimport without problems for a long time, now as mysql 5.7 added json data type support, I'm trying to use mysqlimport with rows containing json data. Here is an example of a row in csv file that will be imported using…
user1011792
4
votes
1 answer

Querying inside json column in laravel elequent

i have a model called orders which have an info json column . in that json i have a property called id . now i want to retrieve all orders with the id of 6 for example . so here is how i have done like below : $order = \DB::table('orders') …
Farshad
  • 1,830
  • 6
  • 38
  • 70
4
votes
1 answer

MySQL create JSON_OBJECT automatically build from wildcard SELECT?

Unfortunately, MySQL doesn't offer a function like SELECT JSON_OBJECT(*) FROM table Now I'm wondering if anybody has already written a function that perform this task. What I want to achieve is to make aSELECT * statement from any given table that…
Lars
  • 920
  • 1
  • 14
  • 34
4
votes
2 answers

how to split a string by delimiter and save it as json object in mysql

So currently I have data in my table like Numbers (Varchar 255) 111|1232|234 434|234 12546|3243|3434 23434|34 35465|3434 Current, the column is VARCHAR but I'm planning to convert it into JSON field so I'm trying to make them json or array before…
Ali Rasheed
  • 2,765
  • 2
  • 18
  • 31
4
votes
1 answer

Append if not exists mysql json field

Is there a JSON function in mysql that will ignore trying to add the element if it already exists? For example: update waitinglist SET new = JSON_ARRAY_APPEND(new, '$', "orange") where id=2; update waitinglist SET new = JSON_ARRAY_APPEND(new, '$',…
David542
  • 104,438
  • 178
  • 489
  • 842
4
votes
1 answer

MySQL JSON array to used in "IN" clause?

Can I use a JSON array as the parameter for an IN clause with MySQL (version 5.7 and greater, as that that supports JSON)? Below is the code that I can't quite get working... DROP TABLE IF EXISTS json_table; CREATE TEMPORARY TABLE…
aero
  • 1,654
  • 1
  • 21
  • 31
4
votes
2 answers

Pop the last item in a JSON Array in MySQL 5.7

I've got an array of dates in a field called from. It can look something like this. ['2016-05-01', '2016-05-03', '2016-05-04'] I want to SELECT the last item (here 2016-05-04). I've tried this: SELECT `from`->"$[JSON_LENGTH(`from`) - 1]" FROM…
Havarem
  • 85
  • 1
  • 6
3
votes
3 answers

Update specific key/value of json object inside json array using MySQL syntax

I have MySQL 5.7.12 DB with a table that has a JSON column. The data in the column as the following structure (json array may contain more than 2 json-objects: [{"ste": "I", "tpe": "PS"}, {"ste": "I", "tpe": "PB"}] I would like to craft an UPDATE…
Shvalb
  • 1,835
  • 2
  • 30
  • 60
3
votes
1 answer

How to use JSON_INSERT with the key that is in string?

I'm not finding a way to use JSON_INSERT in a json whose key is a string: { "computer": { "display": blue }, "computer home":{} } This way it works: JSON_INSERT(type, '$.computer.color', 'red');` But not like this: JSON_INSERT(type,…
3
votes
2 answers

How to query array inside JSON column in Eloquent

I have successfully done some queries using JSON in Eloquent, but I can't figure out how to query a value from an object inside an array like this: In column "attributes", there is : { "products": [ { "media": "1", …
Gilberto Albino
  • 2,572
  • 8
  • 38
  • 50
3
votes
1 answer

Mysql: Update json column with values from different table/column

I've been trying to update the below neighbors column in location table as json object, as seen below, but cannot get this even remotely close to working in any way. Any idea on the query syntax? Original table: location --------------- id name …
Mike
  • 75
  • 7
1 2
3
25 26