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

MySQL JSONField Nested Array SUM

I have a simple table inside a MySQL 8.0 database like this: +-----------+---------+----------+ | id | data |created | +-----------+---------+----------+ | INT | JSON |Timestamp | +-----------+---------+----------+ I can…
drec4s
  • 7,946
  • 8
  • 33
  • 54
1
vote
1 answer

How to use PDO bindParam inside MySQL JSON_CONTAINS?

I'm trying to get the following to run: $driverID = 123; $query = 'SELECT * FROM cars WHERE JSON_CONTAINS(`data`, \'{"drivers": [{"driverID": ":driverID"}]}\');'; $statement = $db->prepare($query); $statement->bindParam(':driverID',…
Optimae
  • 942
  • 1
  • 12
  • 23
1
vote
4 answers

JSON_CONTAINS unable to find from MySQL

In MySQL I'm storing the data within "jsonValues" field with PHP's json_encode: {"product_category":[[{"category":["28"]},{"product":["16","22","64"]}]]} Through PHP, I want to get the data by writing the following query: SELECT * FROM `dbTable`…
1
vote
2 answers

How to delete/remove item from document in json field in mysql laravel

I have a json column named Data in my user table in the database. Example of content: [ { "id": 10, "key": "mail", "type": "male", }, { "id": 5, "key": "name", "type": "female", }, { "id": 8, "key": "mail", …
Chargui Taieb
  • 75
  • 1
  • 10
1
vote
2 answers

querying json column in sql with Laravel

Seofilter db table has json column named category_ids. I wanna query where value exists in json col. And not querying all the row and get their jsoncol value then explode etc, I wanna do it with less query to server because of server loads. for…
Ozal Zarbaliyev
  • 566
  • 6
  • 22
1
vote
0 answers

MySQL extract multiple nested values out of a stored json document as one value

My ultimate goal is I'm building a JSON_OBJECT() select statement that's collecting just a couple values from a stored json value. My current iteration of the select statement: SELECT JSON_OBJECT( 'id', document->>'$.id', 'slug',…
RedactedProfile
  • 2,748
  • 6
  • 32
  • 51
1
vote
1 answer

Filter out unwanted fields from each JSON object of JSON array while reading from MySQL

Lets say that I have a table in my MySQL database with one of the columns of type JSON and I have saved the following JSON in one of the records { "about": "person", "info": [ { "fName": "John", "lName": "Booker", "sex":…
rkudva
  • 167
  • 8
1
vote
0 answers

Mysql Json : Json Column Update error

Recently Migrated Table Column from data type Text to Json. Suppose I have listing table with this table definition and inserted few records in it CREATE TABLE `listing` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `data` json NOT…
Arslan
  • 31
  • 5
1
vote
1 answer

MySQL data is not being displayed in Android RecyclerView (Logcat error: Couldn't load memtrack module)

I have just started developing Android application. I have used XAMPP to generate the database(MYSQL) and php to convert the extracted data into JSON. However, I could not display the following JSON output in Android Recyclerview and there is no…
DreamCrusher
  • 97
  • 1
  • 1
  • 7
1
vote
0 answers

Inserting JSON file content with OPENJSON (SQL 2016) into a table, with one of columns not being part of JSON file

It seems to be pretty straightforward to execute insert and store JSON in SQL table with OPENJSON as long as the content of Json matches table. The problem starts when one of the values I want to insert into a table is not part of the JSON…
LuckyAshnar
  • 355
  • 2
  • 12
1
vote
0 answers

When use MySQL select JSON which sub key is a number

select props->>"$.redirect" from jsonDoc The result is: How to select the 403 just like select props->>"$.redirect.403" from jsonDoc thanks
MrFox
  • 11
  • 3
1
vote
1 answer

mysql JSON_SET or JSON_INSERT function to insert object into nested object if it exists

I have a json type column config I want to add a key value pair into, ultimately looking like: { "features": { "divisions": true, "utilities": { "water": true, "gas": true, "electric": true } } } The issue…
phizzy
  • 936
  • 9
  • 26
1
vote
1 answer

Unable to search using JSON_CONTAINS

I am using the following query to search for a json value named "JavaScript" When I run the query I get 0 results This is my insert command I am using INSERT INTO admin_acc_mngmt_notes (tags) VALUES('["JavaScript"]') This is the search query I am…
json6
  • 85
  • 5
1
vote
1 answer

Filter rows by JSON column that stores array of objects

I have the following table which its called products I omitted columns that I won't need: +---------------------+----------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra …
Murilo
  • 580
  • 5
  • 21
1
vote
2 answers

MySQL JSON extract a single value by id

I have a database with a JSON array in column extra_fields. I need to extract one value by id. The JSONs looks something like this, though the number of objects is random for each row: [ {"id":"1","value":"48768"}, {"id":"2","value"…
Slam
  • 3,125
  • 1
  • 15
  • 24